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

Adding try_err lint #4222

Merged
merged 4 commits into from
Jul 1, 2019
Merged

Adding try_err lint #4222

merged 4 commits into from
Jul 1, 2019

Conversation

jfrikker
Copy link
Contributor

@jfrikker jfrikker commented Jun 21, 2019

changelog: Adds the "try_err" lint, which catches instances of the following: Err(x)?
fixes #4212

@jfrikker jfrikker mentioned this pull request Jun 21, 2019
clippy_lints/src/try_err.rs Show resolved Hide resolved
clippy_lints/src/try_err.rs Show resolved Hide resolved
Copy link
Member

@flip1995 flip1995 left a comment

Choose a reason for hiding this comment

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

LGTM overall.

As a LateLintPass this is a little more complex than expected, but we get correct suggestions with the .into() addition. Great work!

if let ExprKind::Match(ref match_arg, _, MatchSource::TryDesugar) = expr.node;
if let ExprKind::Call(ref match_fun, ref try_args) = match_arg.node;
if let ExprKind::Path(ref match_fun_path) = match_fun.node;
if match_qpath(match_fun_path, &["std", "ops", "Try", "into_result"]);
Copy link
Member

Choose a reason for hiding this comment

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

Can you add this path to clippy_lints/src/utils/path.rs?

// its output type.
fn find_err_return_type<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx ExprKind) -> Option<Ty<'tcx>> {
if let ExprKind::Match(_, ref arms, MatchSource::TryDesugar) = expr {
arms.iter().filter_map(|ty| find_err_return_type_arm(cx, ty)).nth(0)
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't this be written as

Suggested change
arms.iter().filter_map(|ty| find_err_return_type_arm(cx, ty)).nth(0)
arms.iter().find_map(|ty| find_err_return_type_arm(cx, ty))

If so, this should be added as a lint to methods/mod.rs

cx,
TRY_ERR,
expr.span,
&format!("confusing error return, consider using `{}`", suggestion),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
&format!("confusing error return, consider using `{}`", suggestion),
"returning an `Err(_)` with the `?` operator",

expr.span,
"try this",
suggestion,
Applicability::MaybeIncorrect
Copy link
Member

@flip1995 flip1995 Jun 22, 2019

Choose a reason for hiding this comment

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

You should be able to use the utils::span_lint_and_sugg function directly.

Also, do you have an example where this lint suggests something incorrect? If not, you can turn this to MachineApplicable and add // run-rustfix to the test file, like here:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's contrived, but a number of the test cases don't compile after applying the fix. For example:

pub fn basic_test() -> Result<i32, i32> {
    let err: i32 = 1;
    Err(err)?;
    Ok(0)
}

becomes

pub fn basic_test() -> Result<i32, i32> {
    let err: i32 = 1;
    return Err(err);
    Ok(0)
}

and the compiler complains about the unreachable Ok(0). I don't expect that to come up much in real life, but the replacement isn't 100% safe.

Copy link
Member

Choose a reason for hiding this comment

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

But that's just a warning, so it still compiles (except you pass -Dwarnings of course). Also the unreachable_code warning would also apply to the unfixed code, it was just hidden with the weird Err(_)? syntax. So MachineApplicable should be ok here IMO.

Is // run-rustfix tested with -Dwarnings? Otherwise this can be added too.

@flip1995
Copy link
Member

The test file needs formatting.

@flip1995
Copy link
Member

Thanks! the only thing left to do, before merging this, is to run rustfmt on your test file. Don't forget to run update-all-references after that.

@jfrikker
Copy link
Contributor Author

Thanks, should be good to go!

@flip1995
Copy link
Member

flip1995 commented Jul 1, 2019

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 1, 2019

📌 Commit c96bb2c has been approved by flip1995

@bors
Copy link
Collaborator

bors commented Jul 1, 2019

⌛ Testing commit c96bb2c with merge ad638a3...

bors added a commit that referenced this pull request Jul 1, 2019
Adding try_err lint

changelog: Adds the "try_err" lint, which catches instances of the following: Err(x)?
fixes #4212
@bors
Copy link
Collaborator

bors commented Jul 1, 2019

☀️ Test successful - checks-travis, status-appveyor
Approved by: flip1995
Pushing ad638a3 to master...

@bors bors merged commit c96bb2c into rust-lang:master Jul 1, 2019
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.

Error return style
3 participants