Skip to content

Commit 4bf4c47

Browse files
committed
Make restriction lint's use span_lint_and_then (t -> w)
1 parent 7de9c20 commit 4bf4c47

File tree

6 files changed

+92
-72
lines changed

6 files changed

+92
-72
lines changed

clippy_lints/src/matches/match_wild_enum.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::{is_refutable, peel_hir_pat_refs, recurse_or_patterns};
44
use rustc_errors::Applicability;
@@ -148,23 +148,27 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
148148
Applicability::MaybeIncorrect,
149149
),
150150
variants => {
151-
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
152-
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
153-
suggestions.push("_".into());
154-
"wildcard matches known variants and will also match future added variants"
151+
let (message, add_wildcard) = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
152+
(
153+
"wildcard matches known variants and will also match future added variants",
154+
true,
155+
)
155156
} else {
156-
"wildcard match will also match any future added variants"
157+
("wildcard match will also match any future added variants", false)
157158
};
158159

159-
span_lint_and_sugg(
160-
cx,
161-
WILDCARD_ENUM_MATCH_ARM,
162-
wildcard_span,
163-
message,
164-
"try",
165-
suggestions.join(" | "),
166-
Applicability::MaybeIncorrect,
167-
);
160+
span_lint_and_then(cx, WILDCARD_ENUM_MATCH_ARM, wildcard_span, message, |diag| {
161+
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
162+
if add_wildcard {
163+
suggestions.push("_".into());
164+
}
165+
diag.span_suggestion(
166+
wildcard_span,
167+
"try",
168+
suggestions.join(" | "),
169+
Applicability::MaybeIncorrect,
170+
);
171+
});
168172
},
169173
};
170174
}

clippy_lints/src/matches/try_err.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use clippy_utils::{get_parent_expr, is_res_lang_ctor, path_res};
@@ -48,29 +48,28 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
4848
return;
4949
};
5050

51-
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
52-
let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
53-
let mut applicability = Applicability::MachineApplicable;
54-
let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
55-
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
56-
"" // already returns
57-
} else {
58-
"return "
59-
};
60-
let suggestion = if err_ty == expr_err_ty {
61-
format!("{ret_prefix}{prefix}{origin_snippet}{suffix}")
62-
} else {
63-
format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}")
64-
};
65-
66-
span_lint_and_sugg(
51+
span_lint_and_then(
6752
cx,
6853
TRY_ERR,
6954
expr.span,
7055
"returning an `Err(_)` with the `?` operator",
71-
"try",
72-
suggestion,
73-
applicability,
56+
|diag| {
57+
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
58+
let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
59+
let mut applicability = Applicability::MachineApplicable;
60+
let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
61+
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
62+
"" // already returns
63+
} else {
64+
"return "
65+
};
66+
let suggestion = if err_ty == expr_err_ty {
67+
format!("{ret_prefix}{prefix}{origin_snippet}{suffix}")
68+
} else {
69+
format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}")
70+
};
71+
diag.span_suggestion(expr.span, "try", suggestion, applicability);
72+
},
7473
);
7574
}
7675
}

clippy_lints/src/methods/verbose_file_reads.rs

+5-2
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_trait_method;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use rustc_hir::{Expr, ExprKind, QPath};
@@ -23,6 +23,9 @@ pub(super) fn check<'tcx>(
2323
&& matches!(recv.kind, ExprKind::Path(QPath::Resolved(None, _)))
2424
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(recv).peel_refs(), sym::File)
2525
{
26-
span_lint_and_help(cx, VERBOSE_FILE_READS, expr.span, msg, None, help);
26+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
27+
span_lint_and_then(cx, VERBOSE_FILE_READS, expr.span, msg, |diag| {
28+
diag.help(help);
29+
});
2730
}
2831
}

clippy_lints/src/misc_early/unneeded_field_pattern.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::source::snippet_opt;
33
use rustc_ast::ast::{Pat, PatKind};
44
use rustc_lint::EarlyContext;
@@ -21,13 +21,15 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
2121
}
2222
}
2323
if !pfields.is_empty() && wilds == pfields.len() {
24-
span_lint_and_help(
24+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
25+
span_lint_and_then(
2526
cx,
2627
UNNEEDED_FIELD_PATTERN,
2728
pat.span,
2829
"all the struct fields are matched to a wildcard pattern, consider using `..`",
29-
None,
30-
format!("try with `{type_name} {{ .. }}` instead"),
30+
|diag| {
31+
diag.help(format!("try with `{type_name} {{ .. }}` instead"));
32+
},
3133
);
3234
return;
3335
}
@@ -56,14 +58,15 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
5658
}
5759
}
5860

59-
span_lint_and_help(
61+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
62+
span_lint_and_then(
6063
cx,
6164
UNNEEDED_FIELD_PATTERN,
6265
field.span,
63-
"you matched a field with a wildcard pattern, consider using `..` \
64-
instead",
65-
None,
66-
format!("try with `{type_name} {{ {}, .. }}`", normal[..].join(", ")),
66+
"you matched a field with a wildcard pattern, consider using `..` instead",
67+
|diag| {
68+
diag.help(format!("try with `{type_name} {{ {}, .. }}`", normal[..].join(", ")));
69+
},
6770
);
6871
}
6972
}

clippy_lints/src/tests_outside_test_module.rs

+6-4
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_in_cfg_test, is_in_test_function};
33
use rustc_hir::intravisit::FnKind;
44
use rustc_hir::{Body, FnDecl};
@@ -61,13 +61,15 @@ impl LateLintPass<'_> for TestsOutsideTestModule {
6161
&& is_in_test_function(cx.tcx, body.id().hir_id)
6262
&& !is_in_cfg_test(cx.tcx, body.id().hir_id)
6363
{
64-
span_lint_and_note(
64+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
65+
span_lint_and_then(
6566
cx,
6667
TESTS_OUTSIDE_TEST_MODULE,
6768
sp,
6869
"this function marked with #[test] is outside a #[cfg(test)] module",
69-
None,
70-
"move it to a testing module marked with #[cfg(test)]",
70+
|diag| {
71+
diag.note("move it to a testing module marked with #[cfg(test)]");
72+
},
7173
);
7274
}
7375
}

clippy_lints/src/undocumented_unsafe_blocks.rs

+31-22
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::ControlFlow;
22

33
use clippy_config::Conf;
4-
use clippy_utils::diagnostics::span_lint_and_help;
4+
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::is_lint_allowed;
66
use clippy_utils::source::walk_span_to_context;
77
use clippy_utils::visitors::{for_each_expr, Descend};
@@ -129,13 +129,15 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
129129
block.span
130130
};
131131

132-
span_lint_and_help(
132+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
133+
span_lint_and_then(
133134
cx,
134135
UNDOCUMENTED_UNSAFE_BLOCKS,
135136
span,
136137
"unsafe block missing a safety comment",
137-
None,
138-
"consider adding a safety comment on the preceding line",
138+
|diag| {
139+
diag.help("consider adding a safety comment on the preceding line");
140+
},
139141
);
140142
}
141143

@@ -145,13 +147,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
145147
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, tail.span, tail.hir_id)
146148
&& let Some(help_span) = expr_has_unnecessary_safety_comment(cx, tail, pos)
147149
{
148-
span_lint_and_help(
150+
span_lint_and_then(
149151
cx,
150152
UNNECESSARY_SAFETY_COMMENT,
151153
tail.span,
152154
"expression has unnecessary safety comment",
153-
Some(help_span),
154-
"consider removing the safety comment",
155+
|diag| {
156+
diag.span_help(help_span, "consider removing the safety comment");
157+
},
155158
);
156159
}
157160
}
@@ -168,13 +171,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
168171
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id)
169172
&& let Some(help_span) = expr_has_unnecessary_safety_comment(cx, expr, pos)
170173
{
171-
span_lint_and_help(
174+
span_lint_and_then(
172175
cx,
173176
UNNECESSARY_SAFETY_COMMENT,
174177
stmt.span,
175178
"statement has unnecessary safety comment",
176-
Some(help_span),
177-
"consider removing the safety comment",
179+
|diag| {
180+
diag.span_help(help_span, "consider removing the safety comment");
181+
},
178182
);
179183
}
180184
}
@@ -210,13 +214,15 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
210214
item.span
211215
};
212216

213-
span_lint_and_help(
217+
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
218+
span_lint_and_then(
214219
cx,
215220
UNDOCUMENTED_UNSAFE_BLOCKS,
216221
span,
217222
"unsafe impl missing a safety comment",
218-
None,
219-
"consider adding a safety comment on the preceding line",
223+
|diag| {
224+
diag.help("consider adding a safety comment on the preceding line");
225+
},
220226
);
221227
}
222228
},
@@ -225,13 +231,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
225231
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
226232
let (span, help_span) = mk_spans(pos);
227233

228-
span_lint_and_help(
234+
span_lint_and_then(
229235
cx,
230236
UNNECESSARY_SAFETY_COMMENT,
231237
span,
232238
"impl has unnecessary safety comment",
233-
Some(help_span),
234-
"consider removing the safety comment",
239+
|diag| {
240+
diag.span_help(help_span, "consider removing the safety comment");
241+
},
235242
);
236243
}
237244
},
@@ -246,13 +253,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
246253
) {
247254
let (span, help_span) = mk_spans(pos);
248255

249-
span_lint_and_help(
256+
span_lint_and_then(
250257
cx,
251258
UNNECESSARY_SAFETY_COMMENT,
252259
span,
253260
format!("{} has unnecessary safety comment", item.kind.descr()),
254-
Some(help_span),
255-
"consider removing the safety comment",
261+
|diag| {
262+
diag.span_help(help_span, "consider removing the safety comment");
263+
},
256264
);
257265
}
258266
}
@@ -263,13 +271,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
263271
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
264272
let (span, help_span) = mk_spans(pos);
265273

266-
span_lint_and_help(
274+
span_lint_and_then(
267275
cx,
268276
UNNECESSARY_SAFETY_COMMENT,
269277
span,
270278
format!("{} has unnecessary safety comment", item.kind.descr()),
271-
Some(help_span),
272-
"consider removing the safety comment",
279+
|diag| {
280+
diag.span_help(help_span, "consider removing the safety comment");
281+
},
273282
);
274283
}
275284
},

0 commit comments

Comments
 (0)