Skip to content

clippy::single_match does not report if a comment is on top of an arm #14418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ojeda opened this issue Mar 16, 2025 · 2 comments · Fixed by #14420
Closed

clippy::single_match does not report if a comment is on top of an arm #14418

ojeda opened this issue Mar 16, 2025 · 2 comments · Fixed by #14420
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't

Comments

@ojeda
Copy link
Contributor

ojeda commented Mar 16, 2025

Summary

Starting with Rust 1.85.0, clippy::single_match apparently does not get emitted when there is a comment on top of an arm.

Is this intended?

Lint Name

clippy::single_match

Reproducer

I tried this code:

fn _f() {
    match Vec::<i32>::new().try_reserve(42) {
        Err(_) => (),
        // X
        Ok(()) => (),
    }
}

I expected to see this happen: Lint emitted as usual, e.g.

warning: you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
 --> <source>:2:5
  |
2 | /     match Vec::<i32>::new().try_reserve(42) {
3 | |         Err(_) => (),
4 | |         Ok(()) => (),
5 | |     }
  | |_____^ help: try: `if let Err(_) = Vec::<i32>::new().try_reserve(42) { () }`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#single_match
  = note: `#[warn(clippy::single_match)]` on by default

warning: 1 warning emitted

Instead, this happened: No lint emitted.

If one removes the // X line, or if one downgrades to 1.84.0, then the lint does get emitted.

Reproducer: https://godbolt.org/z/K7zoonG8j (Compiler Explorer supports Clippy now! :)

Version

rustc 1.85.0 (4d91de4e4 2025-02-17)
@ojeda ojeda added C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't labels Mar 16, 2025
@samueltardieu
Copy link
Contributor

samueltardieu commented Mar 16, 2025

This (non-lint) was introduced in #13765.

Maybe it would be a good idea to lint, but mark the suggestion as MaybeIncorrect to prevent it from being auto-applied if it would drop comments.

@ojeda
Copy link
Contributor Author

ojeda commented Mar 16, 2025

I see, thanks for the quick reply!

Yeah, if there are comments, then someone should manually inspect it and transform the comments as needed.

ojeda pushed a commit to ojeda/linux that referenced this issue Mar 16, 2025
Extend the Result documentation by some guidelines and examples how
to handle Result error cases gracefully. And how to not handle them.

While at it fix one missing `Result` link in the existing documentation.

[ Moved links out-of-line for improved readability. Fixed `srctree`
  link. Sorted out-of-line links. Added newlines for consistency
  with other docs. Applied paragraph break suggestion. Reworded
  slightly the docs in a couple places. Added Markdown.

  In addition, added `#[allow(clippy::single_match)` for the first
  example. It cannot be an `expect` since due to a difference introduced
  in Rust 1.85.0 when there are comments in the arms of the `match`.
  Reported it upstream, but it was intended:

      rust-lang/rust-clippy#14418

  Perhaps Clippy will lint about it in the future, but without autofix:

      rust-lang/rust-clippy#14420

    - Miguel ]

Link: https://lore.kernel.org/rust-for-linux/CANiq72keOdXy0LFKk9SzYWwSjiD710v=hQO4xi+5E4xNALa6cA@mail.gmail.com/
Signed-off-by: Dirk Behme <[email protected]>
Reviewed-by: Fiona Behrens <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Miguel Ojeda <[email protected]>
github-merge-queue bot pushed a commit that referenced this issue Mar 18, 2025
)

Commit efe3fe9 removed the ability for
`single_match` and `single_match_else` to trigger if comments were
present outside of the arms, as those comments would be lost while
rewriting the `match` expression.

This reinstates the lint, but prevents the suggestion from being applied
automatically in the presence of comments by using the `MaybeIncorrect`
applicability. Also, a note is added to the lint message to warn the
user about the need to preserve the comments if acting upon the
suggestion.

changelog: [`single_match`, `single_match_else`]: reinstate lint when
comments are inside the `match` but do not autofix the code

Fix #14418
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-negative Issue: The lint should have been triggered on code, but wasn't
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants