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

incorrect clippy lint needless_question_mark #7107

Closed
kgv opened this issue Apr 18, 2021 · 3 comments · Fixed by #7165
Closed

incorrect clippy lint needless_question_mark #7107

kgv opened this issue Apr 18, 2021 · 3 comments · Fixed by #7165
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy 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

@kgv
Copy link

kgv commented Apr 18, 2021

Lint name:
needless_question_mark

I tried this code (link to playground):

pub struct Three {
    two: Option<Two>,
}

impl Three {
    pub fn default(&self) -> Option<&Item> {
        Some(self.two.as_deref()?)
    }
    
    // This is clippy suggestion (not working)
    // pub fn default(&self) -> Option<&Item> {
    //     self.two.as_deref()
    // }
}

pub struct Two {
    one: One,
}

impl Deref for Two {
    type Target = One;

    fn deref(&self) -> &Self::Target {
        &self.one
    }
}

pub struct One {
    item: Item,
}

impl Deref for One {
    type Target = Item;

    fn deref(&self) -> &Self::Target {
        &self.item
    }
}

pub struct Item;

In this case, the question mark is needed.
Clippy suggestion doesn't work.

Meta

  • cargo clippy -V:
    clippy 0.1.52 (07e0e2ec 2021-03-24)
    
  • rustc -Vv:
    rustc 1.53.0-nightly (07e0e2ec2 2021-03-24)
    binary: rustc
    commit-hash: 07e0e2ec268c140e607e1ac7f49f145612d0f597
    commit-date: 2021-03-24
    host: x86_64-pc-windows-msvc
    release: 1.53.0-nightly
    LLVM version: 12.0.0
    
@kgv kgv 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 Apr 18, 2021
@giraffate giraffate added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 19, 2021
@camsteffen
Copy link
Contributor

Minimal example

fn foo(s: Option<&String>) -> Option<&str> {
    Some(s?)
}

@ghost
Copy link

ghost commented Apr 22, 2021

So this should be the suggestion?

fn foo(s: Option<&String>) -> Option<&str> {
    s.map(|x| &**x)
}

You still want to lint ? in the terminating expression.

This is lint is saying ? is for flow control not for type coercions.

@camsteffen
Copy link
Contributor

I think that suggestion would have to come from a different (new) lint since the question mark is not "needless".

@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label May 3, 2021
@camsteffen camsteffen self-assigned this May 4, 2021
@bors bors closed this as completed in 65951c9 May 8, 2021
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 good-first-issue These issues are a good way to get started with Clippy 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.

3 participants