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

clippy::question_mark suggests code that's more confusing than before #7967

Open
kangalio opened this issue Nov 13, 2021 · 1 comment
Open
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

Comments

@kangalio
Copy link

kangalio commented Nov 13, 2021

Lint name: clippy::question_mark

I tried this code:

pub struct Message;
pub struct MessageUpdateEvent { content: Option<String> }

pub fn process_message_update(
    user_msg_update: MessageUpdateEvent,
) -> Option<Message> {
    // [some code]
    
    // If message content wasn't touched, don't re-run command
    if user_msg_update.content.is_none() {
        return None;
    }
    
    // [more code]
    
    Some(Message)
}

(for more context, see https://github.com/kangalioo/poise/blob/19bcc0afef2c77533a4e2f0a4aaffa4baea74f2a/src/prefix/track_edits.rs#L85-L92)

I expected to see this happen: no warning is emitted

Why: semantically, the ? operator is meant for propagating exceptional values which the current function doesn't want to deal with. The semantics in this code are different, so usage of ? would be confusing and make it harder to figure what is actually happening here

Instead, this happened: Clippy suggested to replace the check with user_msg_update.content.as_ref()?;

I am not sure how or if Clippy can detect intended semantics of if value.is_none() { return None; }. The best course of action may be to keep this lint as is, if it's not worth it to accommodate this small issue

Meta

Rust version (rustc -Vv):

rustc 1.55.0 (c8dfcfe04 2021-09-06)
binary: rustc
commit-hash: c8dfcfe046a7680554bf4eb612bad840e7631c4b
commit-date: 2021-09-06
host: x86_64-unknown-linux-gnu
release: 1.55.0
LLVM version: 12.0.1
@kangalio kangalio 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 Nov 13, 2021
@kangalio kangalio changed the title clippy::question_mark shouldn't emit when return value isn't used. clippy::question_mark suggests code that's more confusing than before Nov 13, 2021
@dswij
Copy link
Member

dswij commented Nov 19, 2021

question_mark actually checks for these kind of cases, if foo.is_none() { return None; } from the description here.

Although I don't think it's accurate to say it's FP, I can see why one would prefer explicitly writing these out.

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
Projects
None yet
Development

No branches or pull requests

2 participants