Fix #[expect] for default_numeric_fallback

This commit is contained in:
xFrednet 2022-06-09 09:17:46 +02:00
parent 8db734990b
commit 9d201d68b4
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
4 changed files with 45 additions and 32 deletions

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::numeric_literal;
use clippy_utils::source::snippet_opt;
use if_chain::if_chain;
@ -76,7 +76,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
}
/// Check whether a passed literal has potential to cause fallback or not.
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
if_chain! {
if !in_external_macro(self.cx.sess(), lit.span);
if let Some(ty_bound) = self.ty_bounds.last();
@ -101,14 +101,15 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
}
};
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
span_lint_and_sugg(
span_lint_hir_and_then(
self.cx,
DEFAULT_NUMERIC_FALLBACK,
emit_hir_id,
lit.span,
"default numeric fallback might occur",
"consider adding suffix",
sugg,
Applicability::MaybeIncorrect,
|diag| {
diag.span_suggestion(lit.span, "consider adding suffix", sugg, Applicability::MaybeIncorrect);
}
);
}
}
@ -179,7 +180,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
ExprKind::Lit(lit) => {
let ty = self.cx.typeck_results().expr_ty(expr);
self.check_lit(lit, ty);
self.check_lit(lit, ty, expr.hir_id);
return;
},

View file

@ -1,6 +1,7 @@
// run-rustfix
// aux-build:macro_rules.rs
#![feature(lint_reasons)]
#![warn(clippy::default_numeric_fallback)]
#![allow(
unused,
@ -173,4 +174,9 @@ mod in_macro {
}
}
fn check_expect_suppression() {
#[expect(clippy::default_numeric_fallback)]
let x = 21;
}
fn main() {}

View file

@ -1,6 +1,7 @@
// run-rustfix
// aux-build:macro_rules.rs
#![feature(lint_reasons)]
#![warn(clippy::default_numeric_fallback)]
#![allow(
unused,
@ -173,4 +174,9 @@ mod in_macro {
}
}
fn check_expect_suppression() {
#[expect(clippy::default_numeric_fallback)]
let x = 21;
}
fn main() {}

View file

@ -1,5 +1,5 @@
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:20:17
--> $DIR/default_numeric_fallback_i32.rs:21:17
|
LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32`
@ -7,145 +7,145 @@ LL | let x = 22;
= note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:21:18
--> $DIR/default_numeric_fallback_i32.rs:22:18
|
LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:21:21
--> $DIR/default_numeric_fallback_i32.rs:22:21
|
LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:21:24
--> $DIR/default_numeric_fallback_i32.rs:22:24
|
LL | let x = [1, 2, 3];
| ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:22:28
--> $DIR/default_numeric_fallback_i32.rs:23:28
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:22:31
--> $DIR/default_numeric_fallback_i32.rs:23:31
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:22:44
--> $DIR/default_numeric_fallback_i32.rs:23:44
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `3_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:22:47
--> $DIR/default_numeric_fallback_i32.rs:23:47
|
LL | let x = if true { (1, 2) } else { (3, 4) };
| ^ help: consider adding suffix: `4_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:23:23
--> $DIR/default_numeric_fallback_i32.rs:24:23
|
LL | let x = match 1 {
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:24:13
--> $DIR/default_numeric_fallback_i32.rs:25:13
|
LL | 1 => 1,
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:24:18
--> $DIR/default_numeric_fallback_i32.rs:25:18
|
LL | 1 => 1,
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:25:18
--> $DIR/default_numeric_fallback_i32.rs:26:18
|
LL | _ => 2,
| ^ help: consider adding suffix: `2_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:42:21
--> $DIR/default_numeric_fallback_i32.rs:43:21
|
LL | let y = 1;
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:50:21
--> $DIR/default_numeric_fallback_i32.rs:51:21
|
LL | let y = 1;
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:56:21
--> $DIR/default_numeric_fallback_i32.rs:57:21
|
LL | let y = 1;
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:68:9
--> $DIR/default_numeric_fallback_i32.rs:69:9
|
LL | 1
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:74:27
--> $DIR/default_numeric_fallback_i32.rs:75:27
|
LL | let f = || -> _ { 1 };
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:78:29
--> $DIR/default_numeric_fallback_i32.rs:79:29
|
LL | let f = || -> i32 { 1 };
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:92:21
--> $DIR/default_numeric_fallback_i32.rs:93:21
|
LL | generic_arg(1);
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:95:32
--> $DIR/default_numeric_fallback_i32.rs:96:32
|
LL | let x: _ = generic_arg(1);
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:113:28
--> $DIR/default_numeric_fallback_i32.rs:114:28
|
LL | GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:116:36
--> $DIR/default_numeric_fallback_i32.rs:117:36
|
LL | let _ = GenericStruct { x: 1 };
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:134:24
--> $DIR/default_numeric_fallback_i32.rs:135:24
|
LL | GenericEnum::X(1);
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:154:23
--> $DIR/default_numeric_fallback_i32.rs:155:23
|
LL | s.generic_arg(1);
| ^ help: consider adding suffix: `1_i32`
error: default numeric fallback might occur
--> $DIR/default_numeric_fallback_i32.rs:161:21
--> $DIR/default_numeric_fallback_i32.rs:162:21
|
LL | let x = 22;
| ^^ help: consider adding suffix: `22_i32`