Skip to content

Commit 7ac242c

Browse files
committed
Auto merge of rust-lang#13213 - Alexendoo:multispan-sugg, r=y21
Remove `multispan_sugg[_with_applicability]` They're thin wrappers over the corresponding diag method so we should just use that instead changelog: none
2 parents 377d72a + 1ea7bdd commit 7ac242c

14 files changed

+80
-116
lines changed

clippy_lints/src/loops/for_kv_map.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use super::FOR_KV_MAP;
2-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::is_type_diagnostic_item;
55
use clippy_utils::{pat_is_wild, sugg};
6+
use rustc_errors::Applicability;
67
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
78
use rustc_lint::LateContext;
89
use rustc_middle::ty;
@@ -40,13 +41,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
4041
format!("you seem to want to iterate on a map's {kind}s"),
4142
|diag| {
4243
let map = sugg::Sugg::hir(cx, arg, "map");
43-
multispan_sugg(
44-
diag,
44+
diag.multipart_suggestion(
4545
"use the corresponding method",
4646
vec![
4747
(pat_span, snippet(cx, new_pat_span, kind).into_owned()),
4848
(arg_span, format!("{}.{kind}s{mutbl}()", map.maybe_par())),
4949
],
50+
Applicability::MachineApplicable,
5051
);
5152
},
5253
);

clippy_lints/src/loops/manual_while_let_some.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use clippy_utils::{match_def_path, paths, SpanlessEq};
44
use rustc_errors::Applicability;
@@ -38,11 +38,10 @@ fn report_lint(cx: &LateContext<'_>, pop_span: Span, pop_stmt_kind: PopStmt<'_>,
3838
};
3939

4040
let loop_replacement = format!("while let Some({}) = {}.pop()", pat, snippet(cx, receiver_span, ".."));
41-
multispan_sugg_with_applicability(
42-
diag,
41+
diag.multipart_suggestion(
4342
"consider using a `while..let` loop",
43+
vec![(loop_span, loop_replacement), (pop_span, pop_replacement)],
4444
Applicability::MachineApplicable,
45-
[(loop_span, loop_replacement), (pop_span, pop_replacement)],
4645
);
4746
},
4847
);

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::NEEDLESS_RANGE_LOOP;
2-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::has_iter_method;
55
use clippy_utils::visitors::is_local_used;
66
use clippy_utils::{contains_name, higher, is_integer_const, sugg, SpanlessEq};
77
use rustc_ast::ast;
88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9+
use rustc_errors::Applicability;
910
use rustc_hir::def::{DefKind, Res};
1011
use rustc_hir::intravisit::{walk_expr, Visitor};
1112
use rustc_hir::{BinOpKind, BorrowKind, Closure, Expr, ExprKind, HirId, Mutability, Pat, PatKind, QPath};
@@ -145,8 +146,7 @@ pub(super) fn check<'tcx>(
145146
arg.span,
146147
format!("the loop variable `{}` is used to index `{indexed}`", ident.name),
147148
|diag| {
148-
multispan_sugg(
149-
diag,
149+
diag.multipart_suggestion(
150150
"consider using an iterator and enumerate()",
151151
vec![
152152
(pat.span, format!("({}, <item>)", ident.name)),
@@ -155,6 +155,7 @@ pub(super) fn check<'tcx>(
155155
format!("{indexed}.{method}().enumerate(){method_1}{method_2}"),
156156
),
157157
],
158+
Applicability::HasPlaceholders,
158159
);
159160
},
160161
);
@@ -171,10 +172,10 @@ pub(super) fn check<'tcx>(
171172
arg.span,
172173
format!("the loop variable `{}` is only used to index `{indexed}`", ident.name),
173174
|diag| {
174-
multispan_sugg(
175-
diag,
175+
diag.multipart_suggestion(
176176
"consider using an iterator",
177177
vec![(pat.span, "<item>".to_string()), (arg.span, repl)],
178+
Applicability::HasPlaceholders,
178179
);
179180
},
180181
);

clippy_lints/src/loops/unused_enumerate_index.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::UNUSED_ENUMERATE_INDEX;
2-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet;
44
use clippy_utils::{pat_is_wild, sugg};
5+
use rustc_errors::Applicability;
56
use rustc_hir::def::DefKind;
67
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
78
use rustc_lint::LateContext;
@@ -28,13 +29,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>, arg: &Expr<'_
2829
"you seem to use `.enumerate()` and immediately discard the index",
2930
|diag| {
3031
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter");
31-
multispan_sugg(
32-
diag,
32+
diag.multipart_suggestion(
3333
"remove the `.enumerate()` call",
3434
vec![
3535
(pat.span, snippet(cx, elem.span, "..").into_owned()),
3636
(arg.span, base_iter.to_string()),
3737
],
38+
Applicability::MachineApplicable,
3839
);
3940
},
4041
);

clippy_lints/src/manual_strip.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
33
use clippy_utils::consts::{constant, Constant};
4-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
4+
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::source::snippet;
66
use clippy_utils::usage::mutated_variables;
77
use clippy_utils::{eq_expr_value, higher, match_def_path, paths};
88
use rustc_ast::ast::LitKind;
9+
use rustc_errors::Applicability;
910
use rustc_hir::def::Res;
1011
use rustc_hir::intravisit::{walk_expr, Visitor};
1112
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind};
@@ -14,6 +15,7 @@ use rustc_middle::ty;
1415
use rustc_session::impl_lint_pass;
1516
use rustc_span::source_map::Spanned;
1617
use rustc_span::Span;
18+
use std::iter;
1719

1820
declare_clippy_lint! {
1921
/// ### What it does
@@ -108,19 +110,19 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
108110
format!("stripping a {kind_word} manually"),
109111
|diag| {
110112
diag.span_note(test_span, format!("the {kind_word} was tested here"));
111-
multispan_sugg(
112-
diag,
113+
diag.multipart_suggestion(
113114
format!("try using the `strip_{kind_word}` method"),
114-
vec![(
115+
iter::once((
115116
test_span,
116117
format!(
117118
"if let Some(<stripped>) = {}.strip_{kind_word}({}) ",
118119
snippet(cx, target_arg.span, ".."),
119120
snippet(cx, pattern.span, "..")
120121
),
121-
)]
122-
.into_iter()
123-
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
122+
))
123+
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into())))
124+
.collect(),
125+
Applicability::HasPlaceholders,
124126
);
125127
},
126128
);
@@ -183,9 +185,9 @@ fn peel_ref<'a>(expr: &'a Expr<'_>) -> &'a Expr<'a> {
183185
}
184186
}
185187

186-
// Find expressions where `target` is stripped using the length of `pattern`.
187-
// We'll suggest replacing these expressions with the result of the `strip_{prefix,suffix}`
188-
// method.
188+
/// Find expressions where `target` is stripped using the length of `pattern`.
189+
/// We'll suggest replacing these expressions with the result of the `strip_{prefix,suffix}`
190+
/// method.
189191
fn find_stripping<'tcx>(
190192
cx: &LateContext<'tcx>,
191193
strip_kind: StripKind,

clippy_lints/src/matches/match_ref_pats.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::{snippet, walk_span_to_context};
33
use clippy_utils::sugg::Sugg;
44
use core::iter::once;
@@ -54,7 +54,11 @@ where
5454

5555
span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
5656
if !expr.span.from_expansion() {
57-
multispan_sugg(diag, msg, first_sugg.chain(remaining_suggs));
57+
diag.multipart_suggestion(
58+
msg,
59+
first_sugg.chain(remaining_suggs).collect(),
60+
Applicability::MachineApplicable,
61+
);
5862
}
5963
});
6064
}

clippy_lints/src/methods/bind_instead_of_map.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::{contains_return, BIND_INSTEAD_OF_MAP};
2-
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_sugg, span_lint_and_then};
2+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
33
use clippy_utils::peel_blocks;
44
use clippy_utils::source::{snippet, snippet_with_context};
55
use clippy_utils::visitors::find_all_ret_expressions;
@@ -136,15 +136,16 @@ impl BindInsteadOfMap {
136136
return false;
137137
};
138138
span_lint_and_then(cx, BIND_INSTEAD_OF_MAP, expr.span, msg, |diag| {
139-
multispan_sugg_with_applicability(
140-
diag,
141-
"try",
139+
diag.multipart_suggestion(
140+
format!("use `{}` instead", self.good_method_name),
141+
std::iter::once((span, self.good_method_name.into()))
142+
.chain(
143+
suggs
144+
.into_iter()
145+
.map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
146+
)
147+
.collect(),
142148
Applicability::MachineApplicable,
143-
std::iter::once((span, self.good_method_name.into())).chain(
144-
suggs
145-
.into_iter()
146-
.map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
147-
),
148149
);
149150
});
150151
true

clippy_lints/src/methods/unused_enumerate_index.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_hir_and_then};
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::{snippet, snippet_opt};
33
use clippy_utils::{expr_or_init, is_trait_method, pat_is_wild};
44
use rustc_errors::Applicability;
@@ -97,17 +97,16 @@ pub(super) fn check(cx: &LateContext<'_>, call_expr: &Expr<'_>, recv: &Expr<'_>,
9797
enumerate_span,
9898
"you seem to use `.enumerate()` and immediately discard the index",
9999
|diag| {
100-
multispan_sugg_with_applicability(
101-
diag,
100+
diag.multipart_suggestion(
102101
"remove the `.enumerate()` call",
103-
Applicability::MachineApplicable,
104102
vec![
105103
(closure_param.span, new_closure_param),
106104
(
107105
enumerate_span.with_lo(enumerate_recv.span.source_callsite().hi()),
108106
String::new(),
109107
),
110108
],
109+
Applicability::MachineApplicable,
111110
);
112111
},
113112
);

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_self;
33
use clippy_utils::ptr::get_spans;
44
use clippy_utils::source::{snippet, snippet_opt};
@@ -278,9 +278,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
278278
}
279279
}
280280

281-
let spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))];
282-
283-
multispan_sugg(diag, "consider taking a reference instead", spans);
281+
diag.span_suggestion(
282+
input.span,
283+
"consider taking a reference instead",
284+
format!("&{}", snippet(cx, input.span, "_")),
285+
Applicability::MaybeIncorrect,
286+
);
284287
};
285288

286289
span_lint_and_then(

clippy_lints/src/operators/op_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::get_enclosing_block;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::{implements_trait, is_copy};
@@ -64,10 +64,10 @@ pub(crate) fn check<'tcx>(
6464
|diag| {
6565
let lsnip = snippet(cx, l.span, "...").to_string();
6666
let rsnip = snippet(cx, r.span, "...").to_string();
67-
multispan_sugg(
68-
diag,
67+
diag.multipart_suggestion(
6968
"use the values directly",
7069
vec![(left.span, lsnip), (right.span, rsnip)],
70+
Applicability::MachineApplicable,
7171
);
7272
},
7373
);

clippy_lints/src/semicolon_block.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use rustc_errors::Applicability;
44
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -92,11 +92,10 @@ impl SemicolonBlock {
9292
semi_span,
9393
"consider moving the `;` inside the block for consistent formatting",
9494
|diag| {
95-
multispan_sugg_with_applicability(
96-
diag,
95+
diag.multipart_suggestion(
9796
"put the `;` here",
97+
vec![(remove_span, String::new()), (insert_span, ";".to_owned())],
9898
Applicability::MachineApplicable,
99-
[(remove_span, String::new()), (insert_span, ";".to_owned())],
10099
);
101100
},
102101
);
@@ -124,11 +123,10 @@ impl SemicolonBlock {
124123
block.span,
125124
"consider moving the `;` outside the block for consistent formatting",
126125
|diag| {
127-
multispan_sugg_with_applicability(
128-
diag,
126+
diag.multipart_suggestion(
129127
"put the `;` here",
128+
vec![(remove_span, String::new()), (insert_span, ";".to_owned())],
130129
Applicability::MachineApplicable,
131-
[(remove_span, String::new()), (insert_span, ";".to_owned())],
132130
);
133131
},
134132
);

clippy_lints/src/utils/internal_lints/metadata_collector.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ const SUGGESTION_DIAG_METHODS: [(&str, bool); 9] = [
8585
("tool_only_multipart_suggestion", true),
8686
("span_suggestions", true),
8787
];
88-
const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
89-
&["clippy_utils", "diagnostics", "multispan_sugg"],
90-
&["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
91-
];
9288
const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];
9389

9490
/// The index of the applicability name of `paths::APPLICABILITY_VALUES`
@@ -1060,33 +1056,21 @@ impl<'a, 'hir> Visitor<'hir> for IsMultiSpanScanner<'a, 'hir> {
10601056
return;
10611057
}
10621058

1063-
match &expr.kind {
1064-
ExprKind::Call(fn_expr, _args) => {
1065-
let found_function = SUGGESTION_FUNCTIONS
1066-
.iter()
1067-
.any(|func_path| match_function_call(self.cx, fn_expr, func_path).is_some());
1068-
if found_function {
1069-
// These functions are all multi part suggestions
1070-
self.add_single_span_suggestion();
1071-
}
1072-
},
1073-
ExprKind::MethodCall(path, recv, _, _arg_span) => {
1074-
let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(recv));
1075-
if match_type(self.cx, self_ty, &paths::DIAG) {
1076-
let called_method = path.ident.name.as_str().to_string();
1077-
for (method_name, is_multi_part) in &SUGGESTION_DIAG_METHODS {
1078-
if *method_name == called_method {
1079-
if *is_multi_part {
1080-
self.add_multi_part_suggestion();
1081-
} else {
1082-
self.add_single_span_suggestion();
1083-
}
1084-
break;
1059+
if let ExprKind::MethodCall(path, recv, _, _arg_span) = &expr.kind {
1060+
let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(recv));
1061+
if match_type(self.cx, self_ty, &paths::DIAG) {
1062+
let called_method = path.ident.name.as_str().to_string();
1063+
for (method_name, is_multi_part) in &SUGGESTION_DIAG_METHODS {
1064+
if *method_name == called_method {
1065+
if *is_multi_part {
1066+
self.add_multi_part_suggestion();
1067+
} else {
1068+
self.add_single_span_suggestion();
10851069
}
1070+
break;
10861071
}
10871072
}
1088-
},
1089-
_ => {},
1073+
}
10901074
}
10911075

10921076
intravisit::walk_expr(self, expr);

0 commit comments

Comments
 (0)