Skip to content

Some more let_and_return funny-business #11335

Closed
@ijijn

Description

@ijijn

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions