Commit graph

8206 commits

Author SHA1 Message Date
cerdelen
aa2f48b09d Change Test for Issue 14422 (Cognitive Complexity lowered through multiple returns) to test it does'nt regress to false negative 2025-04-13 15:35:18 +02:00
Jason Newcomb
d19c651c62
Remove some clippy::all uses from UI tests (#14600)
It's either unneeded (`warn`/`deny`) or can be replaced by individual
lints (`allow`). Also removes some redundant `allow`s covered by the
default `-Aunused` that I saw along the way

changelog:  none
2025-04-12 22:23:04 +00:00
Timo
b157411dba
Consecutive returns dont decrease cognitive Complexity level anymore (#14460)
changelog: [`cognitive_complexity`]: Consecutive return calls decreased
complexity level of the function by 1.

fixes rust-lang/rust-clippy#14422
2025-04-12 16:53:50 +00:00
Timo
8dfcaa0590
Do not propose to auto-derive Clone in presence of unsafe fields (#14559)
`unsafe_fields` is an incomplete feature; comments have been put near
`#![expect(incomplete_features)]` to ensure that we revisit the
situation when the feature becomes complete.

changelog: [`expl_impl_clone_on_copy`]: do not lint in the presence of
`unsafe` fields

Fixes #14558
2025-04-12 16:50:12 +00:00
Alex Macleod
4b140cb48f Remove some clippy::all uses from UI tests 2025-04-12 13:40:49 +00:00
Samuel Tardieu
ec105bab2f
fix: redundant_clone FP in overlapping lifetime (#14237)
fixes #13900

changelog: [`redundant_clone`]: fix FP in overlapping lifetime
2025-04-11 15:47:20 +00:00
yanglsh
07d8a9d331 fix: redundant_clone FP in overlapping lifetime 2025-04-11 23:42:26 +08:00
Alex Macleod
ac4c69f423
implicit_return: better handling of asynchronous code (#14446)
Blocks created by desugaring will not contain an explicit `return`. Do
not suggest to add it when the user has no control over the desugared
code.

Also, ensure that in a `xxx.await` expression, the suggested `return` is
emitted before the whole expression, not before the `await` keyword.

Fix #14411

changelog: [`implicit_return`]: fix proposed `return` position in the
presence of asynchronous code
2025-04-11 13:39:03 +00:00
cerdelen
c7fbcf4c33 Test for Multiple return statements doesnt decrease cognitive complexity 2025-04-11 09:30:28 +02:00
León Orell Valerian Liehr
15fd2ab73e
Fix a help message of empty_line_after_doc_comments for cases where the following item is nameless 2025-04-10 18:24:45 +02:00
Samuel Tardieu
529bb5f253
Correctly handle bracketed type in default_constructed_unit_struct (#14367)
There were two bugs here. Let's assume `T` is a singleton type
implementing `Default` and that `f()` takes a `T`:

- `f(<T>::default())` cannot be replaced by `f(<T)` as it was (incorrect
spans – this is tricky because the type relative path uses a base span
covering only `T`, not `<T>`) (third commit)
- The argument of `f(<_>::default())` is inferred correctly, but cannot
be replaced by `<_>` or `_`, as this cannot be used to infer an instance
of a singleton type (first commit).

The second commit offers better error messages by pointing at the whole
expression.

Fix #12654

changelog: [`default_constructed_unit_struct`]: do not suggest incorrect
fix when using a type surrounded by brackets
2025-04-10 05:51:39 +00:00
Samuel Tardieu
0aa0d074cd Remove brackets around type name when it is no longer a prefix
When `<T>::default()` is replaced by `T` when `T` is a singleton,
the brackets around the type name can be removed.
2025-04-10 07:46:45 +02:00
Manish Goregaokar
630ac0ca72
Accept self.cmp(other).into() as canonical PartialOrd impl (#14573)
`non_canonical_partial_ord_impl` will now recognize two forms of
canonical implementations: `Some(self.cmp(other))` and
`self.cmp(other).into()`.

changelog: [`non_canonical_partial_ord_impl`]: recognize
`self.cmp(other).into()` as a canonical implementation of
`PartialOrd::partial_cmp()`.

Fixes rust-lang/rust-clippy#13640
2025-04-09 15:23:17 +00:00
Alex Macleod
1cfc95c573
fix: map_entry: don't emit lint before checks have been performed (#14568)
Fixes rust-lang/rust-clippy#14449, introduced in #14314

changelog: [`map_entry`]: fix a false positive where the lint would
trigger without any insert calls present
2025-04-09 12:14:18 +00:00
Samuel Tardieu
e463309f4a
add manual_abs_diff lint (#14482)
changelog: [`manual_abs_diff`]: Initial implementation

Hey, first time writing a new lint for clippy, hope I got it right. I
think it's pretty self-explanatory!
Added a few `fixme` test cases, where the lint can be improved to catch
more (probably rare) patterns, but opening a PR with this initial
implementation to make sure I'm on the right track, and that this lint
is acceptable at all.

😁
2025-04-09 10:29:48 +00:00
Samuel Tardieu
0fe33171e4 Accept self.cmp(other).into() as canonical PartialOrd impl
`non_canonical_partial_ord_impl` will now recognize two forms of
canonical implementations: `Some(self.cmp(other))` and
`self.cmp(other).into()`.
2025-04-09 10:47:41 +02:00
Maja Kądziołka
189b0761c8
fix: map_entry: don't emit lint before checks have been performed
Fixes #14449, introduced in #14314
2025-04-09 04:36:14 +02:00
Samuel Tardieu
1a1ad5e54f
fix: iter_cloned_collect FP with custom From/IntoIterator impl (#14473)
Closes #9119

changelog: [`iter_cloned_collect`]: fix FP with custom
`From`/`IntoIterator` impl
2025-04-08 13:46:46 +00:00
Alex Macleod
634c1c885f
Ensure that peeling does not recurse into macros (#14527)
We do not want to remove casts done inside macros. Also, when printing
the suggestion, take it from the same context as the origin expression
(the root context).

Problems found while working on #14526, but should be merged even if
#14526 is not.

changelog: none
2025-04-08 12:52:36 +00:00
yanglsh
ee36124011 fix: iter_cloned_collect FP with custom From/IntoIterator impl 2025-04-08 20:38:43 +08:00
Samuel Tardieu
cf9cffa114
Fixes for missing_asserts_for_indexing (#14108)
This PR fixes issues with the `missing_asserts_for_indexing` lint.
- false positive when the first index is the highest(or equal) value in
a list of indexes:
```rust
pub fn foo(slice: &[i32]) -> i32{
	slice[1] * slice[0]
}
```
- false negative when an assert statement if found after the indexing
operation.
```rust
pub fn bar(slice: &[i32]) -> i32 {
	let product = slice[0] * slice[1];
	assert!(slice.len() > 1);
	product
}
```

examples: https://godbolt.org/z/s7Y47vKdE

closes: #14079

changelog: [`missing_asserts_for_indexing`]: ignore asserts found after
indexing, and do not require asserts if the first index is highest.
2025-04-06 10:42:29 +00:00
Samuel Tardieu
315ea9590d Do not propose to auto-derive Clone in presence of unsafe fields
`unsafe_fields` is an incomplete feature; comments have been put near
`#![expect(incomplete_features)]` to ensure that we revisit the
situation when the feature becomes complete.
2025-04-06 10:21:40 +02:00
Philipp Krones
ab7e525929
Merge remote-tracking branch 'upstream/master' into rustup 2025-04-03 21:31:02 +02:00
Samuel Tardieu
db4bd96575 Ensure that peeling does not recurse into macros
We do not want to remove casts done inside macros. Also, when printing
the suggestion, take it from the same context as the origin expression
(the root context).
2025-04-03 01:38:53 +02:00
Yotam Ofek
52a3082056 add manual_abs_diff lint 2025-04-02 19:40:14 +00:00
León Orell Valerian Liehr
54994b2d4b
Fix the primary span of redundant_pub_crate when flagging nameless items 2025-04-01 18:20:41 +02:00
Manish Goregaokar
7d3d824d64
Properly handle expansion in single_match (#14495)
Having a macro call as the scrutinee is supported. However, the proposed
suggestion must use the macro call itself, not its expansion.

When the scrutinee is a macro call, do not complain about an irrefutable
match, as the user may not be aware of the result of the macro. A
comparaison will be suggested instead, as if we couldn't see the outcome
of the macro.

Similarly, do not accept macro calls as arm patterns.

changelog: [`single_match`]: proper suggestions in presence of macros

Fixes #14493
2025-03-31 16:42:36 +00:00
Manish Goregaokar
1e78abca67
expand obfuscated_if_else to support {then(), then_some()}.unwrap_or_default() (#14431)
These method chains can be expressed concisely with `if` / `else`.

changelog: [`obfuscated_if_else`]: support `then().unwrap_or_default()`
and `then_some().unwrap_or_default()`
2025-03-31 16:40:32 +00:00
Alex Macleod
f894a81654 Fulfill expectations after first missing-panics-doc 2025-03-31 12:52:06 +00:00
Alex Macleod
6ee4f34741 Abide by allow/expect in bodies for missing_panics_doc 2025-03-31 12:52:06 +00:00
Alex Macleod
c89368537c Fix partially const bodies not linting missing_panics_doc 2025-03-31 12:52:06 +00:00
Jason Newcomb
b46b311ee7
add manual_dangling_ptr lint (#14107)
close #2177

changelog: [`manual_dangling_ptr`]: new lint
2025-03-31 10:14:46 +00:00
Urgau
5f26d0e970 Drop clippy::invalid_null_ptr_usage 2025-03-30 19:33:15 +02:00
y21
c7390279ca add comment and .clone() test case 2025-03-30 14:04:56 +02:00
y21
d5d2189c71 rename lint to char_indices_as_byte_indices 2025-03-30 13:40:03 +02:00
y21
fb32aaf9ac new lint: chars_enumerate_for_byte_indices 2025-03-30 13:39:01 +02:00
Timo
4c610e32ad
fix: unnested_or_patterns suggests wrongly in let (#14401)
Closes #9952

changelog: [`unnested_or_patterns`] fix wrong suggestion in `let`
2025-03-29 13:04:15 +00:00
yanglsh
2df6bba5b0 fix: unnested_or_patterns suggests wrongly in let 2025-03-29 20:52:59 +08:00
Samuel Tardieu
432a8a7a7c Properly handle expansion in single_match
Having a macro call as the scrutinee is supported. However, the proposed
suggestion must use the macro call itself, not its expansion.

When the scrutinee is a macro call, do not complain about an irrefutable
match, as the user may not be aware of the result of the macro. A
comparaison will be suggested instead, as if we couldn't see the outcome
of the macro.

Similarly, do not accept macro calls as arm patterns.
2025-03-28 22:30:09 +01:00
Samuel Tardieu
4ac3611225 implicit_return: do not suggest adding return into desugared code
Blocks created by desugaring will not contain an explicit `return`. Do not
suggest to add it when the user has no control over the desugared code.

Also, in the case of an implicit return on a `.await` expression, ensure
that the suggested `return` is emitted at the right place instead of
before the `await` keyword.
2025-03-28 12:14:43 +01:00
Philipp Krones
c2922d1213
Make collapsible_if recognize the let_chains feature (#14481)
Until `if let` chains are stabilized, we do not collapse them together
or with other `if` expressions unless the `let_chains` feature is
enabled. This is the case for example in Clippy sources.

This was made possible by converting the `collapsible_if` to a late lint
to get access to the set of enabled features. This allows this PR to
supersede #14455 and no longer require an additional configuration
option.

The three commits are, in order:
- a conversion of the existing early lint to a late lint, with no new
feature or tests changes
- the addition of the `let_chains` feature detection and action, and
tests
- the application of the enhanced lint to Clippy sources (136 files
modified)

changelog: [`collapsible_if`]: recognize the rust compiler `let_chains`
feature

r? @flip1995
2025-03-28 09:17:24 +00:00
Samuel Tardieu
01820a9efe Do not make incomplete or invalid suggestions
The `unnecessary_filter_map` and `unnecessary_find_map` lints were
making partial suggestions, proposing to replace the whole expression by
only the method name, or a subexpression which contained explicit
placeholders.

Since even `MaybeIncorrect` suggestions must generate code that
compiles, this changes those lints to recommandation lints with no
code suggestion.
2025-03-27 22:11:09 +01:00
Samuel Tardieu
cd70152470 Make collapsible_if recognize the let_chains feature
Until `if let` chains are stabilized, we do not collapse them together
or with other `if` expressions unless the `let_chains` feature is
enabled. This is the case for example in Clippy sources.
2025-03-27 14:40:44 +01:00
Samuel Tardieu
a895265a4e
Do not warn about shadowing in a destructuring assigment (#14381)
When lowering a destructuring assignment from AST to HIR, the compiler
will reuse the same identifier name (namely `sym::lhs`) for all the
fields. The desugaring must be checked for to avoid a false positive of
the `shadow_unrelated` lint.

Fix #10279
Fix #14377

changelog: [`shadow_unrelated`]: prevent false positive in destructuring
assignments
2025-03-27 11:59:27 +00:00
Samuel Tardieu
d9913dd6ad Do not warn about shadowing in a destructuring assigment
When lowering a destructuring assignment from AST to HIR, the compiler
will reuse the same identifier name (namely `sym::lhs`) for all the
fields. The desugaring must be checked for to avoid a false positive 
of the `shadow_unrelated` lint.
2025-03-27 12:31:54 +01:00
Catherine Flores
3e528672a7
more natural suggestions for cmp_owned (#14247)
Dereferencing string literals in suggestions is redundant.

changelog: [`cmp_owned`]: more natural suggestions are provided for
string literals now

fixes #8103
2025-03-27 00:00:12 +00:00
Samuel Tardieu
9b1945d9fb Prevent including preceeding whitespaces if line contains non blanks
This extra condition prevents a problem when removing the '}' in:
```rust
  ( // There was an opening bracket after the parenthesis, which has been removed
    // This is a comment
   })
```
Removing the whitespaces, including the linefeed, before the '}', would put the
closing parenthesis at the end of the `// This is a comment` line, which would
make it part of the comment as well. In this case, it is best to keep the span
on the '}' alone.
2025-03-26 21:10:09 +01:00
dswij
939d5f93eb
fix: option_if_let_else suggests wrongly when coercion requires explicit cast (#14389)
Closes #11059

changelog: `option_if_let_else`: fix wrong suggestion when coersion
requires explicit cast
2025-03-26 05:08:33 +00:00
dswij
4517b4260f
Fix various typos in lint messages, descriptions and comments (#14459)
changelog: none
2025-03-26 05:05:21 +00:00
Samuel Tardieu
94233fb0ee Unify manual_unwrap_or and manual_unwrap_or_default code
Both lints share a lot of characteristics but were implemented in
unrelated ways. This unifies them, saving around 100 SLOC in the
process, and making one more test trigger the lint. Also, this removes
useless blocks in suggestions.
2025-03-25 18:03:41 +01:00