Skip to content

unnecessary_wraps misses 'return' #6384

@taiki-e

Description

@taiki-e
Member

I tried this code:

fn func(s: &str) -> Option<&'static [&'static str]> {
    Some(match s {
        "a" => &["A"],
        _ => return None,
    })
}

I expected to see this happen: no warning

Instead, this happened:
unnecessary_wraps misses return None and emit warning.

warning: this function's return value is unnecessarily wrapped by `Option`
 --> src/lib.rs:1:1
  |
1 | / fn func(s: &str) -> Option<&'static [&'static str]> {
2 | |     Some(match s {
3 | |         "a" => &["A"],
4 | |         _ => return None,
5 | |     })
6 | | }
  | |_^
  |
  = note: `#[warn(clippy::unnecessary_wraps)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_wraps
help: remove `Option` from the return type...
  |
1 | fn func(s: &str) -> &'static [&'static str] {
  |                     ^^^^^^^^^^^^^^^^^^^^^^^
help: ...and change the returning expressions
  |
2 |     match s {
3 |         "a" => &["A"],
4 |         _ => return None,
5 |     }
  |

Meta

  • cargo clippy -V: clippy 0.0.212 (1c389ff 2020-11-24)
  • rustc -Vv:
    rustc 1.50.0-nightly (1c389ffef 2020-11-24)
    binary: rustc
    commit-hash: 1c389ffeff814726dec325f0f2b0c99107df2673
    commit-date: 2020-11-24
    host: x86_64-apple-darwin
    release: 1.50.0-nightly
    

Mentioning @matsujika who implemented this lint in #6070.

Activity

added
C-bugCategory: Clippy is not doing the correct thing
on Nov 26, 2020
giraffate

giraffate commented on Nov 26, 2020

@giraffate
Contributor

@rustbot modify labels: +L-suggestion-causes-error

added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Nov 26, 2020
hkmatsumoto

hkmatsumoto commented on Nov 26, 2020

@hkmatsumoto
Member

@taiki-e Thanks for filing the issue, it looks like the match needs an arm for hir::ExprKind::Call.
This wasn't the case. Created PR with an alternative solution.

added a commit that references this issue on Nov 28, 2020
6cbb093
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @giraffate@taiki-e@rustbot@hkmatsumoto

      Issue actions

        unnecessary_wraps misses 'return' · Issue #6384 · rust-lang/rust-clippy