Skip to content

Commit cb7f967

Browse files
committed
simplify multispan_sugg interface
- add `multispan_sugg_with_applicability` - not it gets `&str` instead of `String`, like in `diag.multispan_suggestion`
1 parent cfd720d commit cb7f967

File tree

6 files changed

+20
-20
lines changed

6 files changed

+20
-20
lines changed

clippy_lints/src/eq_op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
115115
let rsnip = snippet(cx, r.span, "...").to_string();
116116
multispan_sugg(
117117
diag,
118-
"use the values directly".to_string(),
118+
"use the values directly",
119119
vec![(left.span, lsnip), (right.span, rsnip)],
120120
);
121121
},

clippy_lints/src/loops.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ fn check_for_loop_range<'a, 'tcx>(
11341134
|diag| {
11351135
multispan_sugg(
11361136
diag,
1137-
"consider using an iterator".to_string(),
1137+
"consider using an iterator",
11381138
vec![
11391139
(pat.span, format!("({}, <item>)", ident.name)),
11401140
(
@@ -1163,7 +1163,7 @@ fn check_for_loop_range<'a, 'tcx>(
11631163
|diag| {
11641164
multispan_sugg(
11651165
diag,
1166-
"consider using an iterator".to_string(),
1166+
"consider using an iterator",
11671167
vec![(pat.span, "<item>".to_string()), (arg.span, repl)],
11681168
);
11691169
},

clippy_lints/src/matches.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>
820820

821821
span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
822822
if !expr.span.from_expansion() {
823-
multispan_sugg(diag, msg.to_owned(), suggs);
823+
multispan_sugg(diag, msg, suggs);
824824
}
825825
});
826826
}

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
293293
);
294294
spans.sort_by_key(|&(span, _)| span);
295295
}
296-
multispan_sugg(diag, "consider taking a reference instead".to_string(), spans);
296+
multispan_sugg(diag, "consider taking a reference instead", spans);
297297
};
298298

299299
span_lint_and_then(

clippy_lints/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2206,7 +2206,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {
22062206

22072207
multispan_sugg(
22082208
diag,
2209-
"consider adding a type parameter".to_string(),
2209+
"consider adding a type parameter",
22102210
vec![
22112211
(
22122212
generics_suggestion_span,

clippy_lints/src/utils/diagnostics.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Clippy wrappers around rustc's diagnostic functions.
22
3-
use rustc_errors::{Applicability, CodeSuggestion, DiagnosticBuilder, Substitution, SubstitutionPart, SuggestionStyle};
3+
use rustc_errors::{Applicability, DiagnosticBuilder};
44
use rustc_hir::HirId;
55
use rustc_lint::{LateContext, Lint, LintContext};
66
use rustc_span::source_map::{MultiSpan, Span};
@@ -198,20 +198,20 @@ pub fn span_lint_and_sugg<'a, T: LintContext>(
198198
/// appear once per
199199
/// replacement. In human-readable format though, it only appears once before
200200
/// the whole suggestion.
201-
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I)
201+
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_>, help_msg: &str, sugg: I)
202202
where
203203
I: IntoIterator<Item = (Span, String)>,
204204
{
205-
let sugg = CodeSuggestion {
206-
substitutions: vec![Substitution {
207-
parts: sugg
208-
.into_iter()
209-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
210-
.collect(),
211-
}],
212-
msg: help_msg,
213-
style: SuggestionStyle::ShowCode,
214-
applicability: Applicability::Unspecified,
215-
};
216-
diag.suggestions.push(sugg);
205+
multispan_sugg_with_applicability(diag, help_msg, Applicability::Unspecified, sugg)
206+
}
207+
208+
pub fn multispan_sugg_with_applicability<I>(
209+
diag: &mut DiagnosticBuilder<'_>,
210+
help_msg: &str,
211+
applicability: Applicability,
212+
sugg: I,
213+
) where
214+
I: IntoIterator<Item = (Span, String)>,
215+
{
216+
diag.multipart_suggestion(help_msg, sugg.into_iter().collect(), applicability);
217217
}

0 commit comments

Comments
 (0)