Skip to content

New lint: let _ binding of must use function #4812

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

Closed
flip1995 opened this issue Nov 13, 2019 · 6 comments · Fixed by #4823
Closed

New lint: let _ binding of must use function #4812

flip1995 opened this issue Nov 13, 2019 · 6 comments · Fixed by #4823
Labels
A-lint Area: New lints

Comments

@flip1995
Copy link
Member

Extracted from #4655

A lint that detects let _ bindings of functions, that are must_use:

#[must_use]
fn foo() -> u32 { 42 }

fn bar() -> Result<(), ()> { Ok(()) }

let _ = foo();
let _ = bar();

On how to detect must use #4655 (comment):

We have a check for #[must_use] types in functions.rs already (IIRC is_must_use_ty(cx, ty), we best move this to utils/mod.rs), that you can call with the result of an expr_ty(cx, _) on the initializer.

Originally posted by @flip1995 in #4655 (comment)

@flip1995 flip1995 added the A-lint Area: New lints label Nov 13, 2019
@basil-cow
Copy link
Contributor

I would like to tackle this.

@llogiq
Copy link
Contributor

llogiq commented Nov 13, 2019

Great, go ahead. Tell us if you need anything.

@basil-cow
Copy link
Contributor

Should we not lint Result<T, !> and Result<T, Infallible>? Also, is #[must_use] something like a type modifier, like mut?

@llogiq
Copy link
Contributor

llogiq commented Nov 13, 2019

No, #[must_use] is an annotation that is only used by rustc's unused_must_use lint. And it's easy enough to unwrap() those results; in fact it shouldn't even generate code (I think we should be able to check if the Err type is uninhabited and give a suitable suggestion in those cases).

@basil-cow
Copy link
Contributor

I just realized I assumed a bunch of things. 1. This issue does not talk about Result, even though it's parent does. If we do lint Result, do we lint against let _ = <expr_with_result_ty> or against let _ = <fn_call_returning_result>?. 2. Same goes for #[must_use], issue talks about against let _ = <fn_call_returning_result>, we could also lint against let _ = <expr_with_must_use_ty>

@llogiq
Copy link
Contributor

llogiq commented Nov 16, 2019

Exactly. The lint checks hir::Locals (= let ..) with a wildcard pat and an init expression whose type (via expr_ty) has a must_use annotation.

This will catch, among other things, Result and MutexGuard.

If we want, we could extend the lint to also cover functions/methods directly marked as #[must_use].

bors added a commit that referenced this issue Dec 12, 2019
Add `let_underscore_must_use` lint

changelog: closes #4812 , added a new `let_underscore_must_use` lint, moved `is_must_use_ty` to utils, added `is_must_use_fn` util function
@bors bors closed this as completed in b38b026 Dec 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants