Skip to content

Commit

Permalink
simplify multispan_sugg interface
Browse files Browse the repository at this point in the history
- add `multispan_sugg_with_applicability`
- not it gets `&str` instead of `String`, like in `diag.multispan_suggestion`
  • Loading branch information
alex-700 committed May 16, 2020
1 parent cfd720d commit cb7f967
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/eq_op.rs
Expand Up @@ -115,7 +115,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EqOp {
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(
diag,
"use the values directly".to_string(),
"use the values directly",
vec![(left.span, lsnip), (right.span, rsnip)],
);
},
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/loops.rs
Expand Up @@ -1134,7 +1134,7 @@ fn check_for_loop_range<'a, 'tcx>(
|diag| {
multispan_sugg(
diag,
"consider using an iterator".to_string(),
"consider using an iterator",
vec![
(pat.span, format!("({}, <item>)", ident.name)),
(
Expand Down Expand Up @@ -1163,7 +1163,7 @@ fn check_for_loop_range<'a, 'tcx>(
|diag| {
multispan_sugg(
diag,
"consider using an iterator".to_string(),
"consider using an iterator",
vec![(pat.span, "<item>".to_string()), (arg.span, repl)],
);
},
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/matches.rs
Expand Up @@ -820,7 +820,7 @@ fn check_match_ref_pats(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[Arm<'_>

span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
if !expr.span.from_expansion() {
multispan_sugg(diag, msg.to_owned(), suggs);
multispan_sugg(diag, msg, suggs);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/needless_pass_by_value.rs
Expand Up @@ -293,7 +293,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessPassByValue {
);
spans.sort_by_key(|&(span, _)| span);
}
multispan_sugg(diag, "consider taking a reference instead".to_string(), spans);
multispan_sugg(diag, "consider taking a reference instead", spans);
};

span_lint_and_then(
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/types.rs
Expand Up @@ -2206,7 +2206,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for ImplicitHasher {

multispan_sugg(
diag,
"consider adding a type parameter".to_string(),
"consider adding a type parameter",
vec![
(
generics_suggestion_span,
Expand Down
28 changes: 14 additions & 14 deletions clippy_lints/src/utils/diagnostics.rs
@@ -1,6 +1,6 @@
//! Clippy wrappers around rustc's diagnostic functions.

use rustc_errors::{Applicability, CodeSuggestion, DiagnosticBuilder, Substitution, SubstitutionPart, SuggestionStyle};
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir::HirId;
use rustc_lint::{LateContext, Lint, LintContext};
use rustc_span::source_map::{MultiSpan, Span};
Expand Down Expand Up @@ -198,20 +198,20 @@ pub fn span_lint_and_sugg<'a, T: LintContext>(
/// appear once per
/// replacement. In human-readable format though, it only appears once before
/// the whole suggestion.
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_>, help_msg: String, sugg: I)
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_>, help_msg: &str, sugg: I)
where
I: IntoIterator<Item = (Span, String)>,
{
let sugg = CodeSuggestion {
substitutions: vec![Substitution {
parts: sugg
.into_iter()
.map(|(span, snippet)| SubstitutionPart { snippet, span })
.collect(),
}],
msg: help_msg,
style: SuggestionStyle::ShowCode,
applicability: Applicability::Unspecified,
};
diag.suggestions.push(sugg);
multispan_sugg_with_applicability(diag, help_msg, Applicability::Unspecified, sugg)
}

pub fn multispan_sugg_with_applicability<I>(
diag: &mut DiagnosticBuilder<'_>,
help_msg: &str,
applicability: Applicability,
sugg: I,
) where
I: IntoIterator<Item = (Span, String)>,
{
diag.multipart_suggestion(help_msg, sugg.into_iter().collect(), applicability);
}

0 comments on commit cb7f967

Please sign in to comment.