forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#10467 - blyxyas:underscore_typed, r=Jarcho
Add `let_with_type_underscore` lint Fixes rust-lang#10463 changelog: [`let_with_type_underscore`]: Add the lint.
- Loading branch information
Showing
16 changed files
with
178 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::*; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Detects when a variable is declared with an explicit type of `_`. | ||
/// ### Why is this bad? | ||
/// It adds noise, `: _` provides zero clarity or utility. | ||
/// ### Example | ||
/// ```rust,ignore | ||
/// let my_number: _ = 1; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust,ignore | ||
/// let my_number = 1; | ||
/// ``` | ||
#[clippy::version = "1.69.0"] | ||
pub LET_WITH_TYPE_UNDERSCORE, | ||
complexity, | ||
"unneeded underscore type (`_`) in a variable declaration" | ||
} | ||
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]); | ||
|
||
impl LateLintPass<'_> for UnderscoreTyped { | ||
fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) { | ||
if_chain! { | ||
if !in_external_macro(cx.tcx.sess, local.span); | ||
if let Some(ty) = local.ty; // Ensure that it has a type defined | ||
if let TyKind::Infer = &ty.kind; // that type is '_' | ||
if local.span.ctxt() == ty.span.ctxt(); | ||
then { | ||
span_lint_and_help(cx, | ||
LET_WITH_TYPE_UNDERSCORE, | ||
local.span, | ||
"variable declared with type underscore", | ||
Some(ty.span.with_lo(local.pat.span.hi())), | ||
"remove the explicit type `_` declaration" | ||
) | ||
} | ||
}; | ||
} | ||
} |
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
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
Oops, something went wrong.