Skip to content

Commit d17f113

Browse files
committed
Make restriction lint's use span_lint_and_then (m -> m)
1 parent bc8fc6b commit d17f113

8 files changed

+61
-47
lines changed

clippy_lints/src/drop_forget_ref.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_note;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_must_use_func_call;
33
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
44
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
@@ -126,14 +126,14 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
126126
},
127127
_ => return,
128128
};
129-
span_lint_and_note(
130-
cx,
131-
lint,
132-
expr.span,
133-
msg,
134-
note_span,
135-
format!("argument has type `{arg_ty}`"),
136-
);
129+
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
130+
let note = format!("argument has type `{arg_ty}`");
131+
if let Some(span) = note_span {
132+
diag.span_note(span, note);
133+
} else {
134+
diag.note(note);
135+
}
136+
});
137137
}
138138
}
139139
}

clippy_lints/src/float_literal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
109109
// If the type suffix is missing the suggestion would be
110110
// incorrectly interpreted as an integer so adding a `.0`
111111
// suffix to prevent that.
112-
112+
113113
span_lint_and_then(
114114
cx,
115115
LOSSY_FLOAT_LITERAL,

clippy_lints/src/inherent_impl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! lint on inherent implementations
22
3-
use clippy_utils::diagnostics::span_lint_and_note;
3+
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::is_lint_allowed;
55
use rustc_data_structures::fx::FxHashMap;
66
use rustc_hir::def_id::LocalDefId;
@@ -106,13 +106,14 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
106106
// `TyCtxt::crate_inherent_impls` doesn't have a defined order. Sort the lint output first.
107107
lint_spans.sort_by_key(|x| x.0.lo());
108108
for (span, first_span) in lint_spans {
109-
span_lint_and_note(
109+
span_lint_and_then(
110110
cx,
111111
MULTIPLE_INHERENT_IMPL,
112112
span,
113113
"multiple implementations of this structure",
114-
Some(first_span),
115-
"first implementation here",
114+
|diag| {
115+
diag.span_note(first_span, "first implementation here");
116+
},
116117
);
117118
}
118119
}

clippy_lints/src/methods/map_err_ignore.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use rustc_hir::{CaptureBy, Closure, Expr, ExprKind, PatKind};
44
use rustc_lint::LateContext;
@@ -22,13 +22,18 @@ pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) {
2222
{
2323
// span the area of the closure capture and warn that the
2424
// original error will be thrown away
25-
span_lint_and_help(
25+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
26+
span_lint_and_then(
2627
cx,
2728
MAP_ERR_IGNORE,
2829
fn_decl_span,
2930
"`map_err(|_|...` wildcard pattern discards the original error",
30-
None,
31-
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
31+
|diag| {
32+
diag.help(
33+
34+
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
35+
);
36+
},
3237
);
3338
}
3439
}

clippy_lints/src/missing_assert_message.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_in_test;
33
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, PanicExpn};
44
use rustc_hir::Expr;
@@ -79,13 +79,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingAssertMessage {
7979
};
8080

8181
if let PanicExpn::Empty = panic_expn {
82-
span_lint_and_help(
82+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
83+
span_lint_and_then(
8384
cx,
8485
MISSING_ASSERT_MESSAGE,
8586
macro_call.span,
8687
"assert without any message",
87-
None,
88-
"consider describing why the failing assert is problematic",
88+
|diag| {
89+
diag.help("consider describing why the failing assert is problematic");
90+
},
8991
);
9092
}
9193
}

clippy_lints/src/missing_trait_methods.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_lint_allowed;
33
use clippy_utils::macros::span_is_local;
44
use rustc_hir::def_id::DefIdMap;
@@ -83,15 +83,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods {
8383
cx.tcx.with_stable_hashing_context(|hcx| {
8484
for assoc in provided.values_sorted(&hcx, true) {
8585
let source_map = cx.tcx.sess.source_map();
86-
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
87-
88-
span_lint_and_help(
86+
span_lint_and_then(
8987
cx,
9088
MISSING_TRAIT_METHODS,
9189
source_map.guess_head_span(item.span),
9290
format!("missing trait method provided by default: `{}`", assoc.name),
93-
Some(definition_span),
94-
"implement the method",
91+
|diag| {
92+
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
93+
diag.span_help(definition_span, "implement the method");
94+
},
9595
);
9696
}
9797
});

clippy_lints/src/mixed_read_write_in_expression.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
33
use rustc_hir::intravisit::{walk_expr, Visitor};
44
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, LetStmt, Node, Stmt, StmtKind};
@@ -324,13 +324,17 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
324324
if path_to_local_id(expr, self.var) {
325325
// Check that this is a read, not a write.
326326
if !is_in_assignment_position(self.cx, expr) {
327-
span_lint_and_note(
327+
span_lint_and_then(
328328
self.cx,
329329
MIXED_READ_WRITE_IN_EXPRESSION,
330330
expr.span,
331331
format!("unsequenced read of `{}`", self.cx.tcx.hir().name(self.var)),
332-
Some(self.write_expr.span),
333-
"whether read occurs before this write depends on evaluation order",
332+
|diag| {
333+
diag.span_note(
334+
self.write_expr.span,
335+
"whether read occurs before this write depends on evaluation order",
336+
);
337+
},
334338
);
335339
}
336340
}

clippy_lints/src/module_style.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_ast::ast;
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
@@ -121,17 +121,18 @@ impl EarlyLintPass for ModStyle {
121121
for folder in &folder_segments {
122122
if !mod_folders.contains(folder) {
123123
if let Some((file, path)) = file_map.get(folder) {
124-
let mut correct = path.to_path_buf();
125-
correct.pop();
126-
correct.push(folder);
127-
correct.push("mod.rs");
128-
span_lint_and_help(
124+
span_lint_and_then(
129125
cx,
130126
SELF_NAMED_MODULE_FILES,
131127
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
132128
format!("`mod.rs` files are required, found `{}`", path.display()),
133-
None,
134-
format!("move `{}` to `{}`", path.display(), correct.display(),),
129+
|diag| {
130+
let mut correct = path.to_path_buf();
131+
correct.pop();
132+
correct.push(folder);
133+
correct.push("mod.rs");
134+
diag.help(format!("move `{}` to `{}`", path.display(), correct.display(),));
135+
},
135136
);
136137
}
137138
}
@@ -161,17 +162,18 @@ fn process_paths_for_mod_files<'a>(
161162
/// for code-sharing between tests.
162163
fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
163164
if path.ends_with("mod.rs") && !path.starts_with("tests") {
164-
let mut mod_file = path.to_path_buf();
165-
mod_file.pop();
166-
mod_file.set_extension("rs");
167-
168-
span_lint_and_help(
165+
span_lint_and_then(
169166
cx,
170167
MOD_MODULE_FILES,
171168
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
172169
format!("`mod.rs` files are not allowed, found `{}`", path.display()),
173-
None,
174-
format!("move `{}` to `{}`", path.display(), mod_file.display()),
170+
|diag| {
171+
let mut mod_file = path.to_path_buf();
172+
mod_file.pop();
173+
mod_file.set_extension("rs");
174+
175+
diag.help(format!("move `{}` to `{}`", path.display(), mod_file.display()));
176+
},
175177
);
176178
}
177179
}

0 commit comments

Comments
 (0)