Commit graph

22207 commits

Author SHA1 Message Date
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
Samuel Tardieu
d28d2344d0
correct version attribute for io_other_error (#14282)
r? flip1995

changelog: none
2025-03-31 23:30:39 +00:00
Philipp Krones
6366e31b48
Enable triagebot's merge conflict notifications (#14508)
Since we migrated away from bors I've missed this feature, fortunately
triagebot supports it too -
https://forge.rust-lang.org/triagebot/merge-conflicts.html

r? @flip1995

changelog: none
2025-03-31 17:31:24 +00:00
Philipp Krones
9e6c7af4ba
Suggest ./x test src/tools/clippy --bless when in rust-lang/rust (#14507)
changelog: none
2025-03-31 17:30:19 +00: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
3e837ec25e Enable triagebot's merge conflict notifications 2025-03-31 15:05:45 +00:00
Jason Newcomb
b96fecfee9
Allow #[expect] and #[allow] within function bodies for missing_panics_doc (#14407)
Implements
https://github.com/rust-lang/rust-clippy/issues/11436#issuecomment-2719199421

> [...] I'd really like to be able to (reusing some above examples),
>
> ``` rust
> /// Do something
> pub fn string_from_byte_stream() -> String {
>     let bytes = get_valid_utf8();
> #[expect(clippy::missing_panics_doc_ok, reason="caller can't do
anything about this")]
> String::from_utf8(bytes).expect("`get_valid_utf8()` always returns
valid UTF-8")
> }
> ```

Also fixes an issue where if the first panic is in a `const` context
disables the lint for the rest of the function

The first commit is just moving code around

changelog: [`missing_panics_doc`]: `#[allow]` and `#[expect]` can now be
used within the function body to ignore individual panics
2025-03-31 12:56:49 +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
Alex Macleod
ce39784f13 Move FindPanicUnwrap to missing_headers.rs 2025-03-31 12:52:04 +00:00
Alex Macleod
9172556de8 Suggest ./x test src/tools/clippy --bless when in rust-lang/rust 2025-03-31 12:39:41 +00:00
Jason Newcomb
bb0d09b220
Make visit_map happy path more evident (#14376)
This is a small refactor of `ConfVisitor`'s `visit_map` method.

It adds comments and reduces `match` nesting by adding `continue`
statements.

IMHO, the code is easier to read in this form. No one asked for this, so
I hope the maintainers agree it is an improvement.

changelog: none
2025-03-31 10:18:56 +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
Jason Newcomb
eee7c58eda
use is_automatically_derived() instead of has_attr() (#14506)
`has_attr(def_id, sym::automatically_derived)` is as same as
`is_automatically_derived(def_id)`.

changelog: none
2025-03-31 01:43:04 +00:00
Catherine Flores
4f58673f8d
new lint: char_indices_as_byte_indices (#13435)
Closes #10202.

This adds a new lint that checks for uses of the `.chars().enumerate()`
position in a context where a byte index is required and suggests
changing it to use `.char_indices()` instead.

I'm planning to extend this lint to also detect uses of the position in
iterator chains, e.g. `s.chars().enumerate().for_each(|(i, _)|
s.split_at(i));`, but that's for another time

----------------

changelog: new lint: `chars_enumerate_for_byte_indices`
2025-03-30 17:28:17 +00:00
Philipp Krones
cd67b3b771
Don't hardcode repository in changelog CI (#14500)
I sometimes run lintcheck/CI experiments by opening a PR on my own
clippy fork. This has recently "stopped" working (as in, fails CI) when
the changelog CI was added because the repository is hardcoded as
"rust-lang/rust-clippy", where the PR number won't match with the one on
my repo.

changelog: none

r? flip1995
2025-03-30 14:51:30 +00:00
WeiTheShinobi
2eee361a1a
use is_automatically_derived() instead of has_attr(sym::automatically_derived) 2025-03-30 22:00:01 +08: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
32cf88450b add a note for str::bytes 2025-03-30 13:39:01 +02:00
y21
fb32aaf9ac new lint: chars_enumerate_for_byte_indices 2025-03-30 13:39:01 +02:00
y21
8f3a0db64f don't hardcode repository owner in changelog CI 2025-03-30 00:51:13 +01: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
dswij
bc28421a7a
Take advantage of match ergonomics in clippy_utils::hir_utils (#14499)
This is a style-only change in order to use match ergonomics by removing
unneeded `&` and `ref`.

changelog: none
2025-03-29 12:20:23 +00:00
Samuel Tardieu
bc701925d8 Take advantage of match ergonomics in clippy_utils::hir_utils
This is a style-only change in order to use match ergonomics by removing
unneeded `&` and `ref`.
2025-03-29 12:04:23 +01: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
Alejandra González
d78622eeb8
Make missing_const_for_fn operate on non-optimized MIR (#14003)
The main reason for this is that we might transform MIR in the
optimization passes in a way that doesn't work with const-eval, but it
is irrelevant since const-eval uses another MIR (`mir_for_ctfe`).

Specifically this came up when adding a new check in debug builds
(https://github.com/rust-lang/rust/pull/134424), which is added as part
of an optimization pass.

changelog: none
2025-03-28 11:01:32 +00:00
Bastian Kersting
3e960c12d8 Make missing_const_for_fn operate on non-optimized MIR
This has two reasons: First of all we don't need the optimized MIR for
evaluating the checks of this lint, as const-eval anyways operates on
`mir_for_ctf` which is derived from `mir_drops_elaborated_and_const_checked`.
Second of all we might transform MIR in the optimization passes in a way
that doesn't work with const-eval, but it is irrelevant since const-eval
uses another MIR.

Specifically this came up when adding a new check in debug builds
(https://github.com/rust-lang/rust/pull/134424), which is added as part
of an optimization pass.
2025-03-28 10:46:41 +00: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
Manish Goregaokar
61b38e76b2
Do not make incomplete or invalid suggestions (#14487)
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.

Fixes #14486

changelog: [`unnecessary_find_map`, `unnecessary_filter_map`]: do not
make suggestions that will not compile
2025-03-27 21:15:55 +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
79c69112dc Apply collapsible_if to Clippy itself
Since Clippy uses the `let_chains` feature, there are many occasions to
collapse `if` and `if let` statements.
2025-03-27 14:40:44 +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
9df5177287 Convert the collapsible_if early lint to a late one 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
dswij
764c1b6d16
remove obsolete "Known Problems" in ok_expect (#14458)
It looks like `ok_expect` already
[checks](b27a2bbe26/clippy_lints/src/methods/ok_expect.rs (L16))
for the presence of the `Debug`.

changelog: none
2025-03-27 06:08:16 +00: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
Manish Goregaokar
c4789a014f
Prevent including preceeding whitespaces if line contains non blanks (#14480)
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.

changelog: none

Note to reviewer: `manual_inspect` and `collapsible_if` were two lints
exhibiting this problem in cooked up test cases, which have been
included as non-regression tests.
2025-03-26 22:03:17 +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
Samuel Tardieu
fe4dd5b92e
chore: fix some comments (#14475)
*Please write a short comment explaining your change (or "none" for
internal only changes)*

remove redundant words in comment

changelog: none
2025-03-26 11:35:57 +00:00
todaymoon
a8bfafe929 chore: fix some comments
Signed-off-by: todaymoon <csgcgl@foxmail.com>
2025-03-26 17:40:11 +08: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
Philipp Krones
d09e658677
Apply collapsible if to Clippy sources (#14451)
This PR enables the new ability to collapse `if` statements containing
comments (without losing them) in Clippy sources, excluding tests and
lintcheck, where the default behaviour (no collapsing in presence of
comments) is preserved.

To be applied after #14231. When it is applied, #14455 will be marked as
ready for review, then #14228 afterwards.

changelog: none

r? ghost
2025-03-25 19:26:36 +00:00
Samuel Tardieu
6509de3bfb Fix situations identified by collapsible_if new hits 2025-03-25 19:50:24 +01:00
Manish Goregaokar
1893e1e5e7
Unify manual_unwrap_or and manual_unwrap_or_default code (#14332)
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.

Close #12956

changelog: none
2025-03-25 17:34:00 +00:00