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

Some more let_and_return funny-business #11335

Closed
ijijn opened this issue Aug 14, 2023 · 2 comments · Fixed by #11584
Closed

Some more let_and_return funny-business #11335

ijijn opened this issue Aug 14, 2023 · 2 comments · Fixed by #11584
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@ijijn
Copy link
Contributor

ijijn commented Aug 14, 2023

Summary

Dearest clippyists!

This is maybe one-and-a-half reports on clippy::let_and_return.

The first part involves missing parentheses (the clippy fix doesn't add them but they are given as a subsequent fix in the compiler error).

The second-ish is found in the same code whereby the as _ cast that clippy added as part of the fix can actually be removed for brevity, along with the parentheses that weren't there, so to speak...

Reproducer

I tried this code:

use std::mem::ManuallyDrop;

pub enum DropOrNot<T> {
    DontDrop(ManuallyDrop<T>),
    Drop(T),
}

impl<T> DropOrNot<T> {
    pub fn inner(&self) -> &T {
        let result = match self {
            DropOrNot::DontDrop(x) => x,
            DropOrNot::Drop(x) => x,
        };

        result
    }
}

in lib.rs with cargo clippy --fix

I saw this happen:

(the changed code)

    pub fn inner(&self) -> &T {
        match self {
            DropOrNot::DontDrop(x) => x,
            DropOrNot::Drop(x) => x,
        } as _
    }

(the error message)

after fixes were automatically applied the compiler reported errors within these files:

  * src\lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error: expected expression, found `as`
  --> src\lib.rs:15:11
   |
15 |         } as _
   |           ^^ expected expression
   |
help: parentheses are required to parse this as an expression
   |
12 ~         (match self {
13 |             DropOrNot::DontDrop(x) => x,
14 |             DropOrNot::Drop(x) => x,
15 ~         }) as _
   |

error: aborting due to previous error

Original diagnostics will follow.

warning: returning the result of a `let` binding from a block
  --> src\lib.rs:15:9
   |
10 | /         let result = match self {
11 | |             DropOrNot::DontDrop(x) => x,
12 | |             DropOrNot::Drop(x) => x,
13 | |         };
   | |__________- unnecessary `let` binding
14 |
15 |           result
   |           ^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_and_return
   = note: `#[warn(clippy::let_and_return)]` on by default
help: return the expression directly
   |
10 ~
11 | 
12 ~         match self {
13 +             DropOrNot::DontDrop(x) => x,
14 +             DropOrNot::Drop(x) => x,
15 +         } as _
   |

I expected to see this happen:

In a way, as above, but with parentheses around the match blocks (as suggested) so that the code compiles...

    pub fn inner(&self) -> &T {
        (match self {
            DropOrNot::DontDrop(x) => x,
            DropOrNot::Drop(x) => x,
        }) as _
    }

You'll note that the as _ cast that was added along the way by clippy can also be removed.

    pub fn inner(&self) -> &T {
        match self {
            DropOrNot::DontDrop(x) => x,
            DropOrNot::Drop(x) => x,
        }
    }

Version

rustc 1.73.0-nightly (1b198b3a1 2023-08-13)
binary: rustc
commit-hash: 1b198b3a196442e14fb06978166ab46a4618d131
commit-date: 2023-08-13
host: x86_64-pc-windows-msvc
release: 1.73.0-nightly
LLVM version: 17.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

@ijijn ijijn added the C-bug Category: Clippy is not doing the correct thing label Aug 14, 2023
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Aug 14, 2023
@Centri3
Copy link
Member

Centri3 commented Aug 14, 2023

The Sugg utils type handles this, iirc, so we probably just need to call maybe_par

@ijijn
Copy link
Contributor Author

ijijn commented Aug 15, 2023

Thank you once again for weighing in! Sounds like an plan 👍🏾

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-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.

3 participants