Make restriction lint's use span_lint_and_then (i -> l)

This commit is contained in:
xFrednet 2024-07-21 19:26:53 +02:00
parent 7f0ed11213
commit bc8fc6b1d8
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
7 changed files with 245 additions and 92 deletions

View file

@ -1,6 +1,6 @@
use std::fmt;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::ast::{Expr, ExprKind, InlineAsmOptions};
use rustc_ast::{InlineAsm, Item, ItemKind};
use rustc_lint::{EarlyContext, EarlyLintPass, Lint, LintContext};
@ -49,14 +49,10 @@ fn check_asm_syntax(
};
if style == check_for {
span_lint_and_help(
cx,
lint,
span,
format!("{style} x86 assembly syntax used"),
None,
format!("use {} x86 assembly syntax", !style),
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, lint, span, format!("{style} x86 assembly syntax used"), |diag| {
diag.help(format!("use {} x86 assembly syntax", !style));
});
}
}
}
@ -98,13 +94,13 @@ declare_lint_pass!(InlineAsmX86IntelSyntax => [INLINE_ASM_X86_INTEL_SYNTAX]);
impl EarlyLintPass for InlineAsmX86IntelSyntax {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Intel);
check_asm_syntax(INLINE_ASM_X86_INTEL_SYNTAX, cx, inline_asm, expr.span, AsmStyle::Intel);
}
}
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Intel);
check_asm_syntax(INLINE_ASM_X86_INTEL_SYNTAX, cx, inline_asm, item.span, AsmStyle::Intel);
}
}
}
@ -146,13 +142,13 @@ declare_lint_pass!(InlineAsmX86AttSyntax => [INLINE_ASM_X86_ATT_SYNTAX]);
impl EarlyLintPass for InlineAsmX86AttSyntax {
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::InlineAsm(inline_asm) = &expr.kind {
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, expr.span, AsmStyle::Att);
check_asm_syntax(INLINE_ASM_X86_ATT_SYNTAX, cx, inline_asm, expr.span, AsmStyle::Att);
}
}
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
if let ItemKind::GlobalAsm(inline_asm) = &item.kind {
check_asm_syntax(Self::get_lints()[0], cx, inline_asm, item.span, AsmStyle::Att);
check_asm_syntax(INLINE_ASM_X86_ATT_SYNTAX, cx, inline_asm, item.span, AsmStyle::Att);
}
}
}

View file

@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::numeric_literal;
use rustc_ast::ast::{self, LitFloatType, LitKind};
use rustc_errors::Applicability;
use rustc_errors::{Applicability, SuggestionStyle};
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::{self, FloatTy};
@ -109,29 +109,41 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
// If the type suffix is missing the suggestion would be
// incorrectly interpreted as an integer so adding a `.0`
// suffix to prevent that.
if type_suffix.is_none() {
float_str.push_str(".0");
}
span_lint_and_sugg(
span_lint_and_then(
cx,
LOSSY_FLOAT_LITERAL,
expr.span,
"literal cannot be represented as the underlying type without loss of precision",
"consider changing the type or replacing it with",
numeric_literal::format(&float_str, type_suffix, true),
Applicability::MachineApplicable,
|diag| {
if type_suffix.is_none() {
float_str.push_str(".0");
}
diag.span_suggestion_with_style(
expr.span,
"consider changing the type or replacing it with",
numeric_literal::format(&float_str, type_suffix, true),
Applicability::MachineApplicable,
SuggestionStyle::ShowAlways,
);
},
);
}
} else if digits > max as usize && float_str.len() < sym_str.len() {
span_lint_and_sugg(
span_lint_and_then(
cx,
EXCESSIVE_PRECISION,
expr.span,
"float has excessive precision",
"consider changing the type or truncating it to",
numeric_literal::format(&float_str, type_suffix, true),
Applicability::MachineApplicable,
|diag| {
diag.span_suggestion_with_style(
expr.span,
"consider changing the type or truncating it to",
numeric_literal::format(&float_str, type_suffix, true),
Applicability::MachineApplicable,
SuggestionStyle::ShowAlways,
);
},
);
}
}

View file

@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::root_macro_call_first_node;
use rustc_ast::LitKind;
use rustc_hir::{Expr, ExprKind};
@ -66,16 +66,18 @@ impl LateLintPass<'_> for LargeIncludeFile {
&& (cx.tcx.is_diagnostic_item(sym::include_bytes_macro, macro_call.def_id)
|| cx.tcx.is_diagnostic_item(sym::include_str_macro, macro_call.def_id))
{
span_lint_and_note(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
LARGE_INCLUDE_FILE,
expr.span.source_callsite(),
"attempted to include a large file",
None,
format!(
"the configuration allows a maximum size of {} bytes",
self.max_file_size
),
|diag| {
diag.note(format!(
"the configuration allows a maximum size of {} bytes",
self.max_file_size
));
},
);
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::ty::{implements_trait, is_must_use_ty, match_type};
use clippy_utils::{is_from_proc_macro, is_must_use_func_call, paths};
use rustc_hir::{LetStmt, LocalSource, PatKind};
@ -149,43 +149,53 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
});
if contains_sync_guard {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
LET_UNDERSCORE_LOCK,
local.span,
"non-binding `let` on a synchronization lock",
None,
"consider using an underscore-prefixed named \
binding or dropping explicitly with `std::mem::drop`",
|diag| {
diag.help(
"consider using an underscore-prefixed named \
binding or dropping explicitly with `std::mem::drop`",
);
},
);
} else if let Some(future_trait_def_id) = cx.tcx.lang_items().future_trait()
&& implements_trait(cx, cx.typeck_results().expr_ty(init), future_trait_def_id, &[])
{
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
LET_UNDERSCORE_FUTURE,
local.span,
"non-binding `let` on a future",
None,
"consider awaiting the future or dropping explicitly with `std::mem::drop`",
|diag| {
diag.help("consider awaiting the future or dropping explicitly with `std::mem::drop`");
},
);
} else if is_must_use_ty(cx, cx.typeck_results().expr_ty(init)) {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
LET_UNDERSCORE_MUST_USE,
local.span,
"non-binding `let` on an expression with `#[must_use]` type",
None,
"consider explicitly using expression value",
|diag| {
diag.help("consider explicitly using expression value");
},
);
} else if is_must_use_func_call(cx, init) {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
LET_UNDERSCORE_MUST_USE,
local.span,
"non-binding `let` on a result of a `#[must_use]` function",
None,
"consider explicitly using function result",
|diag| {
diag.help("consider explicitly using function result");
},
);
}
@ -204,18 +214,22 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
return;
}
span_lint_and_help(
span_lint_and_then(
cx,
LET_UNDERSCORE_UNTYPED,
local.span,
"non-binding `let` without a type annotation",
Some(Span::new(
local.pat.span.hi(),
local.pat.span.hi() + BytePos(1),
local.pat.span.ctxt(),
local.pat.span.parent(),
)),
"consider adding a type annotation",
|diag| {
diag.span_help(
Span::new(
local.pat.span.hi(),
local.pat.span.hi() + BytePos(1),
local.pat.span.ctxt(),
local.pat.span.parent(),
),
"consider adding a type annotation",
);
},
);
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_hir as hir;
use rustc_lint::LateContext;
@ -15,13 +15,9 @@ pub(crate) fn check<'tcx>(
&& cx.typeck_results().expr_ty(left).is_integral()
&& cx.typeck_results().expr_ty(right).is_integral()
{
span_lint_and_help(
cx,
INTEGER_DIVISION,
expr.span,
"integer division",
None,
"division of integers may cause loss of precision. consider using floats",
);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, INTEGER_DIVISION, expr.span, "integer division", |diag| {
diag.help("division of integers may cause loss of precision. consider using floats");
});
}
}

View file

@ -2,100 +2,179 @@ error: float has excessive precision
--> tests/ui/excessive_precision.rs:20:26
|
LL | const BAD32_1: f32 = 0.123_456_789_f32;
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79_f32`
| ^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::excessive-precision` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::excessive_precision)]`
help: consider changing the type or truncating it to
|
LL | const BAD32_1: f32 = 0.123_456_79_f32;
| ~~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:21:26
|
LL | const BAD32_2: f32 = 0.123_456_789;
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
| ^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | const BAD32_2: f32 = 0.123_456_79;
| ~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:22:26
|
LL | const BAD32_3: f32 = 0.100_000_000_000_1;
| ^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
| ^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | const BAD32_3: f32 = 0.1;
| ~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:23:29
|
LL | const BAD32_EDGE: f32 = 1.000_000_9;
| ^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.000_001`
| ^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | const BAD32_EDGE: f32 = 1.000_001;
| ~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:27:26
|
LL | const BAD64_3: f64 = 0.100_000_000_000_000_000_1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | const BAD64_3: f64 = 0.1;
| ~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:30:22
|
LL | println!("{:?}", 8.888_888_888_888_888_888_888);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `8.888_888_888_888_89`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | println!("{:?}", 8.888_888_888_888_89);
| ~~~~~~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:41:22
|
LL | let bad32: f32 = 1.123_456_789;
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8`
| ^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad32: f32 = 1.123_456_8;
| ~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:42:26
|
LL | let bad32_suf: f32 = 1.123_456_789_f32;
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
| ^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad32_suf: f32 = 1.123_456_8_f32;
| ~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:43:21
|
LL | let bad32_inf = 1.123_456_789_f32;
| ^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8_f32`
| ^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad32_inf = 1.123_456_8_f32;
| ~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:53:36
|
LL | let bad_vec32: Vec<f32> = vec![0.123_456_789];
| ^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_79`
| ^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad_vec32: Vec<f32> = vec![0.123_456_79];
| ~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:54:36
|
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_789];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0.123_456_789_123_456_78`
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad_vec64: Vec<f64> = vec![0.123_456_789_123_456_78];
| ~~~~~~~~~~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:58:24
|
LL | let bad_e32: f32 = 1.123_456_788_888e-10;
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8e-10`
| ^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad_e32: f32 = 1.123_456_8e-10;
| ~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:61:27
|
LL | let bad_bige32: f32 = 1.123_456_788_888E-10;
| ^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `1.123_456_8E-10`
| ^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let bad_bige32: f32 = 1.123_456_8E-10;
| ~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:70:13
|
LL | let _ = 2.225_073_858_507_201_1e-308_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `2.225_073_858_507_201e-308_f64`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let _ = 2.225_073_858_507_201e-308_f64;
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:73:13
|
LL | let _ = 1.000_000_000_000_001e-324_f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `0_f64`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | let _ = 0_f64;
| ~~~~~
error: float has excessive precision
--> tests/ui/excessive_precision.rs:83:20
|
LL | const _: f64 = 3.0000000000000000e+00;
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or truncating it to: `3.0`
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or truncating it to
|
LL | const _: f64 = 3.0;
| ~~~
error: aborting due to 16 previous errors

View file

@ -2,70 +2,124 @@ error: literal cannot be represented as the underlying type without loss of prec
--> tests/ui/lossy_float_literal.rs:14:18
|
LL | let _: f32 = 16_777_217.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_216.0`
| ^^^^^^^^^^^^
|
= note: `-D clippy::lossy-float-literal` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::lossy_float_literal)]`
help: consider changing the type or replacing it with
|
LL | let _: f32 = 16_777_216.0;
| ~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:15:18
|
LL | let _: f32 = 16_777_219.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
| ^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f32 = 16_777_220.0;
| ~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:16:18
|
LL | let _: f32 = 16_777_219.;
| ^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
| ^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f32 = 16_777_220.0;
| ~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:17:18
|
LL | let _: f32 = 16_777_219.000;
| ^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
| ^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f32 = 16_777_220.0;
| ~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:18:13
|
LL | let _ = 16_777_219f32;
| ^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220_f32`
| ^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _ = 16_777_220_f32;
| ~~~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:19:19
|
LL | let _: f32 = -16_777_219.0;
| ^^^^^^^^^^^^ help: consider changing the type or replacing it with: `16_777_220.0`
| ^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f32 = -16_777_220.0;
| ~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:21:18
|
LL | let _: f64 = 9_007_199_254_740_993.0;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f64 = 9_007_199_254_740_992.0;
| ~~~~~~~~~~~~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:22:18
|
LL | let _: f64 = 9_007_199_254_740_993.;
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
| ^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f64 = 9_007_199_254_740_992.0;
| ~~~~~~~~~~~~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:23:18
|
LL | let _: f64 = 9_007_199_254_740_993.00;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f64 = 9_007_199_254_740_992.0;
| ~~~~~~~~~~~~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:24:13
|
LL | let _ = 9_007_199_254_740_993f64;
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992_f64`
| ^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _ = 9_007_199_254_740_992_f64;
| ~~~~~~~~~~~~~~~~~~~~~~~~~
error: literal cannot be represented as the underlying type without loss of precision
--> tests/ui/lossy_float_literal.rs:25:19
|
LL | let _: f64 = -9_007_199_254_740_993.0;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: consider changing the type or replacing it with: `9_007_199_254_740_992.0`
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider changing the type or replacing it with
|
LL | let _: f64 = -9_007_199_254_740_992.0;
| ~~~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 11 previous errors