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

Question mark lint #2410

Merged
merged 2 commits into from
Jan 31, 2018
Merged

Question mark lint #2410

merged 2 commits into from
Jan 31, 2018

Conversation

gnieto
Copy link
Contributor

@gnieto gnieto commented Jan 29, 2018

Lint which suggest the usage of the question mark operator when it can be used. At the moment, it checks for calls to is_none, followed by a return None;.

This PR (partially) solves: #2358

I've coded only the case described on the linked issue, but it should be possible to lint for other pieces of code that may use the question mark, like the following one:

use std::io;
use std::io::Read;
use std::fs::File;

fn read_file() -> Result<String, io::Error> {
    let f = File::open("file.txt");
    let mut f = match f {
        Ok(file) => file,
        Err(e) => return Err(e),
    };

    let mut s = String::new();
    match f.read_to_string(&mut s) {
        Ok(_) => Ok(s),
        Err(e) => Err(e),
    }
}

error: this block may be rewritten with the `?` operator
--> $DIR/question_mark.rs:2:5
|
2 | if a.is_none() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not the correct span. It should hilight the entire if a.is_none() { return None; }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed the span. Can you check it now?

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jup, this is perfect now.

@oli-obk oli-obk merged commit 6f90f58 into rust-lang:master Jan 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants