Skip to content

Commit 8ab2f88

Browse files
committed
Move AsUnderscore into Casts lint pass
1 parent 868dba9 commit 8ab2f88

File tree

6 files changed

+62
-81
lines changed

6 files changed

+62
-81
lines changed

clippy_lints/src/as_underscore.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use clippy_utils::diagnostics::span_lint_and_then;
2+
use rustc_errors::Applicability;
3+
use rustc_hir::{Expr, Ty, TyKind};
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty;
6+
7+
use super::AS_UNDERSCORE;
8+
9+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, ty: &'tcx Ty<'_>) {
10+
if matches!(ty.kind, TyKind::Infer) {
11+
span_lint_and_then(cx, AS_UNDERSCORE, expr.span, "using `as _` conversion", |diag| {
12+
let ty_resolved = cx.typeck_results().expr_ty(expr);
13+
if let ty::Error(_) = ty_resolved.kind() {
14+
diag.help("consider giving the type explicitly");
15+
} else {
16+
diag.span_suggestion(
17+
ty.span,
18+
"consider giving the type explicitly",
19+
ty_resolved,
20+
Applicability::MachineApplicable,
21+
);
22+
}
23+
});
24+
}
25+
}

clippy_lints/src/casts/mod.rs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod as_underscore;
12
mod cast_abs_to_unsigned;
23
mod cast_enum_constructor;
34
mod cast_lossless;
@@ -506,6 +507,34 @@ declare_clippy_lint! {
506507
"casting the result of `abs()` to an unsigned integer can panic"
507508
}
508509

510+
declare_clippy_lint! {
511+
/// ### What it does
512+
/// Check for the usage of `as _` conversion using inferred type.
513+
///
514+
/// ### Why is this bad?
515+
/// The conversion might include lossy conversion and dangerous cast that might go
516+
/// undetected due to the type being inferred.
517+
///
518+
/// The lint is allowed by default as using `_` is less wordy than always specifying the type.
519+
///
520+
/// ### Example
521+
/// ```rust
522+
/// fn foo(n: usize) {}
523+
/// let n: u16 = 256;
524+
/// foo(n as _);
525+
/// ```
526+
/// Use instead:
527+
/// ```rust
528+
/// fn foo(n: usize) {}
529+
/// let n: u16 = 256;
530+
/// foo(n as usize);
531+
/// ```
532+
#[clippy::version = "1.63.0"]
533+
pub AS_UNDERSCORE,
534+
restriction,
535+
"detects `as _` conversion"
536+
}
537+
509538
pub struct Casts {
510539
msrv: Option<RustcVersion>,
511540
}
@@ -534,7 +563,8 @@ impl_lint_pass!(Casts => [
534563
PTR_AS_PTR,
535564
CAST_ENUM_TRUNCATION,
536565
CAST_ENUM_CONSTRUCTOR,
537-
CAST_ABS_TO_UNSIGNED
566+
CAST_ABS_TO_UNSIGNED,
567+
AS_UNDERSCORE,
538568
]);
539569

540570
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -547,8 +577,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
547577
return;
548578
}
549579

550-
if let ExprKind::Cast(cast_expr, cast_to) = expr.kind {
551-
if is_hir_ty_cfg_dependant(cx, cast_to) {
580+
if let ExprKind::Cast(cast_expr, cast_to_hir) = expr.kind {
581+
if is_hir_ty_cfg_dependant(cx, cast_to_hir) {
552582
return;
553583
}
554584
let (cast_from, cast_to) = (
@@ -575,6 +605,8 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
575605
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
576606
cast_enum_constructor::check(cx, expr, cast_expr, cast_from);
577607
}
608+
609+
as_underscore::check(cx, expr, cast_to_hir);
578610
}
579611

580612
cast_ref_to_mut::check(cx, expr);

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ store.register_lints(&[
3838
almost_complete_letter_range::ALMOST_COMPLETE_LETTER_RANGE,
3939
approx_const::APPROX_CONSTANT,
4040
as_conversions::AS_CONVERSIONS,
41-
as_underscore::AS_UNDERSCORE,
4241
asm_syntax::INLINE_ASM_X86_ATT_SYNTAX,
4342
asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX,
4443
assertions_on_constants::ASSERTIONS_ON_CONSTANTS,
@@ -69,6 +68,7 @@ store.register_lints(&[
6968
cargo::REDUNDANT_FEATURE_NAMES,
7069
cargo::WILDCARD_DEPENDENCIES,
7170
case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
71+
casts::AS_UNDERSCORE,
7272
casts::CAST_ABS_TO_UNSIGNED,
7373
casts::CAST_ENUM_CONSTRUCTOR,
7474
casts::CAST_ENUM_TRUNCATION,

clippy_lints/src/lib.register_restriction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
66
LintId::of(as_conversions::AS_CONVERSIONS),
7-
LintId::of(as_underscore::AS_UNDERSCORE),
87
LintId::of(asm_syntax::INLINE_ASM_X86_ATT_SYNTAX),
98
LintId::of(asm_syntax::INLINE_ASM_X86_INTEL_SYNTAX),
109
LintId::of(assertions_on_result_states::ASSERTIONS_ON_RESULT_STATES),
1110
LintId::of(attrs::ALLOW_ATTRIBUTES_WITHOUT_REASON),
11+
LintId::of(casts::AS_UNDERSCORE),
1212
LintId::of(casts::FN_TO_NUMERIC_CAST_ANY),
1313
LintId::of(create_dir::CREATE_DIR),
1414
LintId::of(dbg_macro::DBG_MACRO),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ mod renamed_lints;
170170
mod almost_complete_letter_range;
171171
mod approx_const;
172172
mod as_conversions;
173-
mod as_underscore;
174173
mod asm_syntax;
175174
mod assertions_on_constants;
176175
mod assertions_on_result_states;
@@ -923,7 +922,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
923922
store.register_early_pass(move || Box::new(almost_complete_letter_range::AlmostCompleteLetterRange::new(msrv)));
924923
store.register_late_pass(|| Box::new(swap_ptr_to_ref::SwapPtrToRef));
925924
store.register_late_pass(|| Box::new(mismatching_type_param_order::TypeParamMismatch));
926-
store.register_late_pass(|| Box::new(as_underscore::AsUnderscore));
927925
store.register_late_pass(|| Box::new(read_zero_byte_vec::ReadZeroByteVec));
928926
store.register_late_pass(|| Box::new(default_instead_of_iter_empty::DefaultIterEmpty));
929927
store.register_late_pass(move || Box::new(manual_rem_euclid::ManualRemEuclid::new(msrv)));

0 commit comments

Comments
 (0)