Commit graph

10162 commits

Author SHA1 Message Date
yanglsh
222ebd0535 fix: search_is_some suggests wrongly inside macro 2025-07-14 22:38:04 +08:00
Jason Newcomb
1c64211aee
Fix manual is multiple of (#15205)
Fix several issues with `manual_is_multiple_of`

- `&a % &b == 0` compiles, but requires dereferencing `b` when replacing
with `a.is_multiple_of(b)`.
- In `a % b == 0`, if type of `a` is not certain, `a.is_multiple_of(b)`
might not be typable.
- In `a % b == 0`, `a` and `b` must be unsigned integers, not any
arbitrary types implementing `Rem` and outputing an integer.

Those fixes have required increasing the precision of type certainty
determination in the two first patches.

Fixes rust-lang/rust-clippy#15203
Fixes rust-lang/rust-clippy#15204

changelog: [`manual_is_multiple_of`]: fix various false positive
2025-07-14 08:33:27 +00:00
Samuel Tardieu
ba947c5728
Fix manual_assert suggests wrongly for macros (#15264)
Closes rust-lang/rust-clippy#15227

changelog: [`manual_assert`] fix wrong suggestions for macros
2025-07-14 07:55:25 +00:00
Jason Newcomb
d9acd919ad
fix false negative of expect_used (#15253)
Fixes: rust-lang/rust-clippy#15247

changelog: Fix the false negative of [`expect_used`] when metting
`Option::ok().expect`.
2025-07-14 07:52:48 +00:00
yanglsh
901ab5b8bd fix: manual_assert suggests wrongly for macros 2025-07-14 00:19:05 +08:00
yanglsh
9d964048ec fix: manual_abs_diff suggests wrongly behind refs 2025-07-13 21:23:51 +08:00
Alejandra González
7e2d26f4b2
Fix approx_const for some new cases (#15236)
Fix some cases for `approx_const`:
- When the literal has more digits than significant ones, the string
comparison wasn't working
- When numbers are formatted in a non trivial way (with leading `0`s or
using `_`) the lint now finds those cases

changelog: [`approx_const`]: Fix when used over overly precise literals
and non trivially formatted numerals

Fixes rust-lang/rust-clippy#15194
2025-07-12 13:57:16 +00:00
Ada Alakbarova
e610584a9a
{flat_,}map_identity: also recognize |[x, y]| [x, y] 2025-07-12 14:02:41 +02:00
Teodoro Freund
093b6e5de9 Fix approx_const for some new cases
Improved naming and comment
2025-07-12 07:16:23 +01:00
relaxcn
1d1b97d514 fix false negative of expect_used 2025-07-12 01:44:55 +08:00
Samuel Tardieu
488f4dd04d
fix legacy_numeric_constants suggestion when call is wrapped in parens (#15191)
Fixes rust-lang/rust-clippy#15008

changelog: [`legacy_numeric_constants`]: fix suggestion when call is
inside parentheses like `(i32::max_value())`
2025-07-11 05:28:23 +00:00
Alejandra González
b5e701efc5
arithmetic_side_effects: don't warn on NonZeroU*.get() - 1 (#15238)
changelog: [`arithmetic_side_effects`]: don't warn on `NonZeroU*.get() -
1`

fixes rust-lang/rust-clippy#15225
2025-07-10 22:57:30 +00:00
Ada Alakbarova
a745e2cae3
fix(arithmetic_side_effects): don't warn on NonZeroU*.get() - 1 2025-07-11 00:40:38 +02:00
llogiq
df5a0ee919
or_fun_call: lint method calls inside map_or first arg (#15074)
<strike>blocked on rust-lang/rust-clippy#15073</strike>

Lint method calls inside `map_or` too, so for this, lint will be showed:
```rust
Some(4).map_or("asd".to_string().len() as i32, f);
```
previously it worked only for:
```rust
Some(4).map_or(slow_fun(), f);
```

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=bfcda42a6af446e69bc883a8b45eb13c

Sorry for multiple `or_fun_call` PRs.

changelog: [`or_fun_call`]: lint method calls inside map_or first arg
2025-07-10 21:27:09 +00:00
llogiq
4dcaa80651
Fix few typos (#15218)
Fix few typos

changelog: none
2025-07-10 21:25:45 +00:00
Samuel Tardieu
ae6416aa86 add extra legacy_numeric_constants test cases 2025-07-10 11:56:54 -07:00
Dan Johnson
e30313cc6b fix legacy_numeric_constants suggestion when call is wrapped in parens 2025-07-10 11:56:23 -07:00
Philipp Krones
9e0f749e08
Merge remote-tracking branch 'upstream/master' into rustup 2025-07-10 20:01:08 +02:00
llogiq
49ca220ca0
Fix multiple problems in #15063 (#15070)
Closes rust-lang/rust-clippy#15063

----

changelog: [`op_ref`]: fix wrongly showed macro definition in
suggestions
changelog: [`needless_bool_assign`]: fix missing curlies when on else if
2025-07-09 21:55:36 +00:00
Timo
012e5f538b
Fix manual_is_variant_and condition generation (#15206)
When comparing `x.map(func) == Some(bool_lit)`, the value of `bool_lit`
was ignored, despite the fact that its value should determine the value
of the proposed expression.

`func` can be either a closure or a path. For the latter, η-expansion
will be used if needed to invert the result of the function call.

changelog: [`manual_is_variant_and`]: fix inverted suggestions that
could lead to code with different semantics

Fixes rust-lang/rust-clippy#15202

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

-
[Beta-nomination](https://github.com/rust-lang/rust-clippy/pull/15206#issuecomment-3034006613)
by [samueltardieu](https://github.com/samueltardieu)

*Managed by `@rustbot`—see
[help](https://forge.rust-lang.org/triagebot/note.html) for details*

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
2025-07-09 21:04:18 +00:00
Jason Newcomb
a24fb386b1
Do not remove method call if type is adjusted (#15181)
Propose to replace `x.map_or(false, |y| y == z)` by `x == Some(z)` only
if `x` is not adjusted. Otherwise, the type of `x` in the comparaison
may not be the expected one, as it may be the product of an auto-deref.

changelog: [`unnecessary_map_or`]: do not propose to replace the
`map_or` call by a comparaison if types wouldn't be correct

Fixes rust-lang/rust-clippy#15180
2025-07-09 15:04:39 +00:00
Esteban Küber
5228e28469 Account for const stability in clippy when checking constness 2025-07-07 23:07:32 +00:00
Esteban Küber
d6f7aad00e Make Default const and add some const Default impls
Full list of `impl const Default` types:

- ()
- bool
- char
- Cell
- std::ascii::Char
- usize
- u8
- u16
- u32
- u64
- u128
- i8
- i16
- i32
- i64
- i128
- f16
- f32
- f64
- f128
- std::marker::PhantomData<T>
- Option<T>
- std::iter::Empty<T>
- std::ptr::Alignment
- &[T]
- &mut [T]
- &str
- &mut str
- String
- Vec<T>
2025-07-07 22:09:37 +00:00
Ses Goe
5853e6fa7e
chore: add tests to check against the --test compile flag 2025-07-07 15:11:30 -04:00
Jared Davis
1f36d4df93 run cargo dev fmt on changes 2025-07-07 11:08:45 -04:00
Jared Davis
2b93d2cca6 skip exit late lint pass on tests
When using the `--test` or `--all-targets` flag, the exit lint should not fail on the main function.
2025-07-07 10:38:29 -04:00
Alexey Semenyuk
a743efc0e5 Fix few typos 2025-07-07 11:50:03 +05:00
Samuel Tardieu
d8ba94b0ae
Fix manual_is_variant_and condition generation
When comparing `x.map(func) == Some(bool_lit)`, the value of `bool_lit` was
ignored, despite the fact that its value should determine the value of
the proposed expression.

`func` can be either a closure or a path. For the latter, η-expansion
will be used if needed to invert the result of the function call.
2025-07-06 23:36:45 +02:00
klensy
addf309a5a also lint method calls inside map_or first arg 2025-07-06 18:45:29 +03:00
klensy
cc9d96cebc precommit test 2025-07-06 18:45:29 +03:00
llogiq
c943f4c0e9
or_fun_call: also lint and method for Option/Result (#15073)
<strike>build on top of rust-lang/rust-clippy#15071</strike>

This also adds ability to lint Option/Result::and method. Yes, this is
not `or` method, but uses the same eager/lazy linting logic. Should i
update lint description to list all checked structs/methods?

changelog: [`or_fun_call`]: lint Option/Result::and
2025-07-06 15:31:13 +00:00
llogiq
cc7b2f57e0
Properly check that an expression might be the one returned (#15115)
The `TyCtxt::hir_get_fn_id_for_return_block()` function was too broad,
as it will return positively even when given part of an expression that
can be used as a return value. A new
`potential_return_of_enclosing_body()` utility function has been made to
represent the fact that an expression might be directly returned from
its enclosing body.

changelog: [`return_and_then`]: prevent false positives in case of a
partially used expression

Fixes rust-lang/rust-clippy#15111
Fixes rust-lang/rust-clippy#14927

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_SUMMARY_START -->

### Summary Notes

-
[Beta-nomination](https://github.com/rust-lang/rust-clippy/pull/15115#issuecomment-2996222332)
by [samueltardieu](https://github.com/samueltardieu)

*Managed by `@rustbot`—see
[help](https://forge.rust-lang.org/triagebot/note.html) for details*

<!-- TRIAGEBOT_SUMMARY_END -->
<!-- TRIAGEBOT_END -->
2025-07-06 15:24:46 +00:00
llogiq
5c51a1d9d3
Handle potentially-shadowing bindings in manual_let_else (#15118)
This commit also adds more test cases, which already worked but were
mentioned in the issue.

Fixes rust-lang/rust-clippy#9939

changelog: [`manual_let_else`]: correctly handle binding subpattern with
unused name
2025-07-06 15:22:46 +00:00
llogiq
412def25d5
fix: Improve floating point lint to handle ambiguous type (#15133)
Fixes: rust-lang/rust-clippy#14897, rust-lang/rust-clippy#10015

This is not a perfect solution, but it fixes the issue. Happy to discuss
further with the clippy team.

This PR refines check_mul_add to avoid false positives on ambiguous
float types (e.g., f32 vs f64). It improves type detection and ensures
mul_add is only suggested when the type is clear.

Changes:
- Improved detection of ambiguous float types
- Refined mul_add suggestion logic
- Added tests for ambiguous cases
## Example

Before this change, expressions like:
```rust
let x = 1.0; // ambiguous type
let _ = x * 2.0 + 0.5; // could incorrectly suggest mul_add
```

----

changelog: [`suboptimal_flops`]: improve handling of ambiguous float
types in mul_add suggestions
2025-07-06 15:18:52 +00:00
llogiq
60d57851ad
Don't remove explicit cast to trait object pointer (#15145)
Fixes rust-lang/rust-clippy#15141

----

changelog: [`borrow_as_ptr`]: do not remove cast to trait object pointer
2025-07-06 15:15:35 +00:00
llogiq
cd679d6b84
FIX: NegMultiply should preserve parenthesis when method is called (#15179)
Hi,

I noticed that the lint
[neg_multiply](https://rust-lang.github.io/rust-clippy/master/index.html#neg_multiply)
generates bad code when we call a method on the expression.
Consider `((a.delta - 0.5).abs() * -1.0).total_cmp(&1.0)`. Currently
this would be changed by clippy to `-(a.delta - 0.5).abs()
.total_cmp(&1.0)` - which does not compile because we are trying to
negate an ordering enum - but what we really want is `(-(a.delta -
0.5).abs()).total_cmp(&1.0)`.

This PR fixes this.

changelog: [`neg_multiply`] does not remove parenthesis anymore if a
method is being called on the affected expression

NOTE: This is the first time I am contributing to clippy or the rust
repo in general. So I am not sure whether my approach to fixing this
issue is goo, if there are better solutions or if I missed something.

Thanks & hope you have a good day,
Dario
2025-07-06 15:14:39 +00:00
llogiq
2713c509de
fix: redundant_closure_call lint for closures with block (#15144)
Fixes rust-lang/rust-clippy#9583

When a closure with no parameters is called immediately after being
defined, clippy can now suggest replacing the entire expression with
just the closure's body.

----

changelog: [`redundant_closure_call`]: add fixes for closures with block
2025-07-06 15:10:25 +00:00
dswij
8d688c6f60
Do not remove as if it changes the type (#15182)
While `expr as T` can be removed as a statement if `expr` has no
side-effect, the `as T` part alone cannot be removed if the type of
`expr` would be ambiguous without the cast.

changelog: [`unnecessary_operation`]: do not remove casts if they are
useful to type the expression

Fixes rust-lang/rust-clippy#15173
2025-07-06 09:58:44 +00:00
blyxyas
e0e881f873 Propagate accept-comment-above-attributes to statements
Turns out that things like `#[attr]return unsafe { func(); }` didn't work
because we weren't checking above attributes on statements, only on blocks!
2025-07-06 02:23:50 +02:00
Samuel Tardieu
a8ab02c464
Fix several issues with manual_is_multiple_of
- `&a % &b == 0` compiles, but requires dereferencing `b` when
  replacing with `a.is_multiple_of(b)`.
- In `a % b == 0`, if type of `a` is not certain,
  `a.is_multiple_of(b)` might not be typable.
- In `a % b == 0`, `a` and `b` must be unsigned integers, not any
  arbitrary types implementing `Rem` and outputing an integer.
2025-07-03 22:14:00 +02:00
Scott Schafer
7060377bfe refactor: Make -Ztrack-diagnostics emit like a note 2025-07-03 07:19:25 -06:00
Samuel Tardieu
8bc4add579
Do not lint intrinsics as empty loops 2025-07-03 13:12:40 +02:00
Samuel Tardieu
8e4b5448cf
use ui_test dependency builder for test dependencies (#14883)
changelog: none

This tries to make progress on
https://github.com/rust-lang/rust/issues/78717 by using the ui_test
dependency handling instead of linking in the dependencies of clippy
itself with the tests. This partially reverts
https://github.com/rust-lang/rust-clippy/pull/11045. However, we still
use the old style of dealing with dependencies for clippy's own crates
and the "internal" tests, as otherwise those would get rebuilt which
takes too long.
2025-07-02 13:44:39 +00:00
Samuel Tardieu
8bd580b80e Add track_caller attributes to trace origin of Clippy lints
This allows the use of `-Z track-diagnostics` to see the origin of
Clippy lints emission, as is already the case for lints coming from
rustc.
2025-07-02 11:50:25 +02:00
Samuel Tardieu
ebdbd7cfc0
doc_nested_refdefs: do not falsely report checkboxes as refdefs (#15146)
changelog: [`doc_nested_refdefs`]: fix false positive where [task
lists](http://pulldown-cmark.github.io/pulldown-cmark/third_party/gfm_tasklist.html)
are reported as refdefs
2025-07-01 22:31:15 +00:00
Guillaume Gomez
4793fd1c46 Update clippy source code to make use of TyCtxt::def_descr instead of ItemKind::descr 2025-07-01 17:27:48 +02:00
yanglsh
9eb8512255 fix: std-instead-of-core FP when not all items come from the new crate 2025-07-01 22:46:24 +08:00
Ralf Jung
5ff1a4164c use ui_test dependency builder for non-internal test dependencies 2025-07-01 13:27:49 +02:00
Dario Heinisch
e7fa5364c6 fix tests (run cargo uibless after fmt) 2025-07-01 10:51:18 +02:00
Dario Heinisch
708ea873b1 run cargo dev fmt 2025-07-01 10:46:27 +02:00