-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
actually add files update lints change to pedantic
- Loading branch information
Showing
9 changed files
with
277 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use if_chain::if_chain; | ||
use rustc::declare_lint_pass; | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc_session::declare_tool_lint; | ||
|
||
use crate::utils::{is_must_use_func_call, is_must_use_ty, span_help_and_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for `let _ = <expr>` | ||
/// where expr is #[must_use] | ||
/// | ||
/// **Why is this bad?** It's better to explicitly | ||
/// handle the value of a #[must_use] expr | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust | ||
/// fn f() -> Result<u32, u32> { | ||
/// Ok(0) | ||
/// } | ||
/// | ||
/// let _ = f(); | ||
/// // is_ok() is marked #[must_use] | ||
/// let _ = f().is_ok(); | ||
/// ``` | ||
pub LET_UNDERSCORE_MUST_USE, | ||
restriction, | ||
"non-binding let on a #[must_use] expression" | ||
} | ||
|
||
declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_MUST_USE]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LetUnderscore { | ||
fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &Stmt) { | ||
if_chain! { | ||
if let StmtKind::Local(ref local) = stmt.kind; | ||
if let PatKind::Wild = local.pat.kind; | ||
if let Some(ref init) = local.init; | ||
then { | ||
if is_must_use_ty(cx, cx.tables.expr_ty(init)) { | ||
span_help_and_lint( | ||
cx, | ||
LET_UNDERSCORE_MUST_USE, | ||
stmt.span, | ||
"non-binding let on an expression with #[must_use] type", | ||
"consider explicitly using expression value" | ||
) | ||
} else if is_must_use_func_call(cx, init) { | ||
span_help_and_lint( | ||
cx, | ||
LET_UNDERSCORE_MUST_USE, | ||
stmt.span, | ||
"non-binding let on a result of a #[must_use] function", | ||
"consider explicitly using function result" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#![warn(clippy::let_underscore_must_use)] | ||
|
||
#[must_use] | ||
fn f() -> u32 { | ||
0 | ||
} | ||
|
||
fn g() -> Result<u32, u32> { | ||
Ok(0) | ||
} | ||
|
||
#[must_use] | ||
fn l<T>(x: T) -> T { | ||
x | ||
} | ||
|
||
fn h() -> u32 { | ||
0 | ||
} | ||
|
||
struct S {} | ||
|
||
impl S { | ||
#[must_use] | ||
pub fn f(&self) -> u32 { | ||
0 | ||
} | ||
|
||
pub fn g(&self) -> Result<u32, u32> { | ||
Ok(0) | ||
} | ||
|
||
fn k(&self) -> u32 { | ||
0 | ||
} | ||
|
||
#[must_use] | ||
fn h() -> u32 { | ||
0 | ||
} | ||
|
||
fn p() -> Result<u32, u32> { | ||
Ok(0) | ||
} | ||
} | ||
|
||
trait Trait { | ||
#[must_use] | ||
fn a() -> u32; | ||
} | ||
|
||
impl Trait for S { | ||
fn a() -> u32 { | ||
0 | ||
} | ||
} | ||
|
||
fn main() { | ||
let _ = f(); | ||
let _ = g(); | ||
let _ = h(); | ||
let _ = l(0_u32); | ||
|
||
let s = S {}; | ||
|
||
let _ = s.f(); | ||
let _ = s.g(); | ||
let _ = s.k(); | ||
|
||
let _ = S::h(); | ||
let _ = S::p(); | ||
|
||
let _ = S::a(); | ||
|
||
let _ = if true { Ok(()) } else { Err(()) }; | ||
|
||
let a = Result::<(), ()>::Ok(()); | ||
|
||
let _ = a.is_ok(); | ||
|
||
let _ = a.map(|_| ()); | ||
|
||
let _ = a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
error: non-binding let on a result of a #[must_use] function | ||
--> $DIR/let_underscore.rs:46:5 | ||
| | ||
LL | let _ = f(); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::let-underscore-must-use` implied by `-D warnings` | ||
= help: consider explicitly using function result | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:47:5 | ||
| | ||
LL | let _ = g(); | ||
| ^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: non-binding let on a result of a #[must_use] function | ||
--> $DIR/let_underscore.rs:51:5 | ||
| | ||
LL | let _ = s.f(); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using function result | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:52:5 | ||
| | ||
LL | let _ = s.g(); | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: non-binding let on a result of a #[must_use] function | ||
--> $DIR/let_underscore.rs:54:5 | ||
| | ||
LL | let _ = S::h(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using function result | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:55:5 | ||
| | ||
LL | let _ = S::p(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: non-binding let on a result of a #[must_use] function | ||
--> $DIR/let_underscore.rs:57:5 | ||
| | ||
LL | let _ = S::a(); | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using function result | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:59:5 | ||
| | ||
LL | let _ = if true { Ok(()) } else { Err(()) }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: non-binding let on a result of a #[must_use] function | ||
--> $DIR/let_underscore.rs:63:5 | ||
| | ||
LL | let _ = a.is_ok(); | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using function result | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:65:5 | ||
| | ||
LL | let _ = a.map(|_| ()); | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: non-binding let on an expression with #[must_use] type | ||
--> $DIR/let_underscore.rs:67:5 | ||
| | ||
LL | let _ = a; | ||
| ^^^^^^^^^^ | ||
| | ||
= help: consider explicitly using expression value | ||
|
||
error: aborting due to 11 previous errors | ||
|