Skip to content
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

needless_match bad suggestion with unsize coercion #8551

Closed
ijackson opened this issue Mar 16, 2022 · 0 comments · Fixed by #8549
Closed

needless_match bad suggestion with unsize coercion #8551

ijackson opened this issue Mar 16, 2022 · 0 comments · Fixed by #8549
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ijackson
Copy link

Summary

needless_match can suggest replacing a match statement with an arm implying a coercion, with a simple expression which no longer does the coercion.

Lint Name

needless_match

Reproducer

I tried this code:

trait Trait { }
struct Struct;
impl Trait for Struct { }

fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
    match s {
        Some(s) => Some(s),
        None => None,
    }
}

fn main() {
    optmap(None);
}

I saw this happen:

nailing-cargo: invoking: cargo clippy --manifest-path=/home/ian/Rustup/Arti/experiments/Cargo.toml --locked --target-dir=target --offline
    Checking foo v0.1.0 (/home/ian/Rustup/Arti/experiments)
warning: this match expression is unnecessary
 --> src/main.rs:6:5
  |
6 | /     match s {
7 | |         Some(s) => Some(s),
8 | |         None => None,
9 | |     }
  | |_____^ help: replace it with: `s`
  |
  = note: `#[warn(clippy::needless_match)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_match

warning: `foo` (bin "foo") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
nailing-cargo: unnailed.  status 0.

I expected to see this happen:

Either no complaint, or, better, suggesting something like

fn optmap(s: Option<&Struct>) -> Option<&dyn Trait> {
    s.map(|s| s as _)
}

Version

rustc 1.61.0-nightly (52b34550a 2022-03-15)
binary: rustc
commit-hash: 52b34550aca5f7dd7e152f773e3ab786acb86f6f
commit-date: 2022-03-15
host: x86_64-unknown-linux-gnu
release: 1.61.0-nightly
LLVM version: 14.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@ijackson ijackson added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 16, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 16, 2022
J-ZhengLi added a commit to J-ZhengLi/rust-clippy that referenced this issue Mar 17, 2022
fabricedesre pushed a commit to capyloon/arti that referenced this issue Apr 5, 2022
The match was doing a deref coercion.

Found by current nightly's needless_match (whose suggestion doesn't
compile, rust-lang/rust-clippy#8551 )
bors added a commit that referenced this issue Apr 6, 2022
fix FP in lint `[needless_match]`

fixes: #8542
fixes: #8551
fixes: #8595
fixes: #8599

---

changelog: check for more complex custom type, and ignore type coercion in [`needless_match`]
@bors bors closed this as completed in 4b12862 Apr 6, 2022
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-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants