-
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.
Auto merge of #8934 - DevAccentor:as_underscore, r=Manishearth
add [`as_underscore`] lint closes #8847 detect usage of `as _` and enforce the usage of explicit type like ```rust fn foo(n: usize) {} let n: u16 = 256; foo(n as _); ``` will suggest to change to ```rust fn foo(n: usize) {} let n: u16 = 256; foo(n as usize); ``` changelog: add [`as_underscore`] lint
- Loading branch information
Showing
8 changed files
with
125 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then}; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind, TyKind}; | ||
use rustc_lint::{LateContext, LateLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_middle::ty; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Check for the usage of `as _` conversion using inferred type. | ||
/// | ||
/// ### Why is this bad? | ||
/// The conversion might include lossy conversion and dangerous cast that might go | ||
/// undetected du to the type being inferred. | ||
/// | ||
/// The lint is allowed by default as using `_` is less wordy than always specifying the type. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// fn foo(n: usize) {} | ||
/// let n: u16 = 256; | ||
/// foo(n as _); | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// fn foo(n: usize) {} | ||
/// let n: u16 = 256; | ||
/// foo(n as usize); | ||
/// ``` | ||
#[clippy::version = "1.63.0"] | ||
pub AS_UNDERSCORE, | ||
restriction, | ||
"detects `as _` conversion" | ||
} | ||
declare_lint_pass!(AsUnderscore => [AS_UNDERSCORE]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for AsUnderscore { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) { | ||
if in_external_macro(cx.sess(), expr.span) { | ||
return; | ||
} | ||
|
||
if let ExprKind::Cast(_, ty) = expr.kind && let TyKind::Infer = ty.kind { | ||
|
||
let ty_resolved = cx.typeck_results().expr_ty(expr); | ||
if let ty::Error(_) = ty_resolved.kind() { | ||
span_lint_and_help( | ||
cx, | ||
AS_UNDERSCORE, | ||
expr.span, | ||
"using `as _` conversion", | ||
None, | ||
"consider giving the type explicitly", | ||
); | ||
} else { | ||
span_lint_and_then( | ||
cx, | ||
AS_UNDERSCORE, | ||
expr.span, | ||
"using `as _` conversion", | ||
|diag| { | ||
diag.span_suggestion( | ||
ty.span, | ||
"consider giving the type explicitly", | ||
format!("{}", ty_resolved), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
); | ||
} | ||
} | ||
} | ||
} |
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,13 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::as_underscore)] | ||
|
||
fn foo(_n: usize) {} | ||
|
||
fn main() { | ||
let n: u16 = 256; | ||
foo(n as usize); | ||
|
||
let n = 0_u128; | ||
let _n: u8 = n as u8; | ||
} |
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,13 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::as_underscore)] | ||
|
||
fn foo(_n: usize) {} | ||
|
||
fn main() { | ||
let n: u16 = 256; | ||
foo(n as _); | ||
|
||
let n = 0_u128; | ||
let _n: u8 = n as _; | ||
} |
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,20 @@ | ||
error: using `as _` conversion | ||
--> $DIR/as_underscore.rs:9:9 | ||
| | ||
LL | foo(n as _); | ||
| ^^^^^- | ||
| | | ||
| help: consider giving the type explicitly: `usize` | ||
| | ||
= note: `-D clippy::as-underscore` implied by `-D warnings` | ||
|
||
error: using `as _` conversion | ||
--> $DIR/as_underscore.rs:12:18 | ||
| | ||
LL | let _n: u8 = n as _; | ||
| ^^^^^- | ||
| | | ||
| help: consider giving the type explicitly: `u8` | ||
|
||
error: aborting due to 2 previous errors | ||
|