Skip to content

Commit

Permalink
Auto merge of rust-lang#13213 - Alexendoo:multispan-sugg, r=y21
Browse files Browse the repository at this point in the history
Remove `multispan_sugg[_with_applicability]`

They're thin wrappers over the corresponding diag method so we should just use that instead

changelog: none
  • Loading branch information
bors committed Aug 4, 2024
2 parents 377d72a + 1ea7bdd commit 7ac242c
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 116 deletions.
7 changes: 4 additions & 3 deletions clippy_lints/src/loops/for_kv_map.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use super::FOR_KV_MAP;
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{pat_is_wild, sugg};
use rustc_errors::Applicability;
use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
use rustc_lint::LateContext;
use rustc_middle::ty;
Expand Down Expand Up @@ -40,13 +41,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
format!("you seem to want to iterate on a map's {kind}s"),
|diag| {
let map = sugg::Sugg::hir(cx, arg, "map");
multispan_sugg(
diag,
diag.multipart_suggestion(
"use the corresponding method",
vec![
(pat_span, snippet(cx, new_pat_span, kind).into_owned()),
(arg_span, format!("{}.{kind}s{mutbl}()", map.maybe_par())),
],
Applicability::MachineApplicable,
);
},
);
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/loops/manual_while_let_some.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{match_def_path, paths, SpanlessEq};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -38,11 +38,10 @@ fn report_lint(cx: &LateContext<'_>, pop_span: Span, pop_stmt_kind: PopStmt<'_>,
};

let loop_replacement = format!("while let Some({}) = {}.pop()", pat, snippet(cx, receiver_span, ".."));
multispan_sugg_with_applicability(
diag,
diag.multipart_suggestion(
"consider using a `while..let` loop",
vec![(loop_span, loop_replacement), (pop_span, pop_replacement)],
Applicability::MachineApplicable,
[(loop_span, loop_replacement), (pop_span, pop_replacement)],
);
},
);
Expand Down
11 changes: 6 additions & 5 deletions clippy_lints/src/loops/needless_range_loop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::NEEDLESS_RANGE_LOOP;
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::ty::has_iter_method;
use clippy_utils::visitors::is_local_used;
use clippy_utils::{contains_name, higher, is_integer_const, sugg, SpanlessEq};
use rustc_ast::ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_hir::{BinOpKind, BorrowKind, Closure, Expr, ExprKind, HirId, Mutability, Pat, PatKind, QPath};
Expand Down Expand Up @@ -145,8 +146,7 @@ pub(super) fn check<'tcx>(
arg.span,
format!("the loop variable `{}` is used to index `{indexed}`", ident.name),
|diag| {
multispan_sugg(
diag,
diag.multipart_suggestion(
"consider using an iterator and enumerate()",
vec![
(pat.span, format!("({}, <item>)", ident.name)),
Expand All @@ -155,6 +155,7 @@ pub(super) fn check<'tcx>(
format!("{indexed}.{method}().enumerate(){method_1}{method_2}"),
),
],
Applicability::HasPlaceholders,
);
},
);
Expand All @@ -171,10 +172,10 @@ pub(super) fn check<'tcx>(
arg.span,
format!("the loop variable `{}` is only used to index `{indexed}`", ident.name),
|diag| {
multispan_sugg(
diag,
diag.multipart_suggestion(
"consider using an iterator",
vec![(pat.span, "<item>".to_string()), (arg.span, repl)],
Applicability::HasPlaceholders,
);
},
);
Expand Down
7 changes: 4 additions & 3 deletions clippy_lints/src/loops/unused_enumerate_index.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::UNUSED_ENUMERATE_INDEX;
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::{pat_is_wild, sugg};
use rustc_errors::Applicability;
use rustc_hir::def::DefKind;
use rustc_hir::{Expr, ExprKind, Pat, PatKind};
use rustc_lint::LateContext;
Expand All @@ -28,13 +29,13 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &Pat<'tcx>, arg: &Expr<'_
"you seem to use `.enumerate()` and immediately discard the index",
|diag| {
let base_iter = sugg::Sugg::hir(cx, self_arg, "base iter");
multispan_sugg(
diag,
diag.multipart_suggestion(
"remove the `.enumerate()` call",
vec![
(pat.span, snippet(cx, elem.span, "..").into_owned()),
(arg.span, base_iter.to_string()),
],
Applicability::MachineApplicable,
);
},
);
Expand Down
22 changes: 12 additions & 10 deletions clippy_lints/src/manual_strip.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use clippy_config::msrvs::{self, Msrv};
use clippy_config::Conf;
use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet;
use clippy_utils::usage::mutated_variables;
use clippy_utils::{eq_expr_value, higher, match_def_path, paths};
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
use rustc_hir::def::Res;
use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind};
Expand All @@ -14,6 +15,7 @@ use rustc_middle::ty;
use rustc_session::impl_lint_pass;
use rustc_span::source_map::Spanned;
use rustc_span::Span;
use std::iter;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -108,19 +110,19 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
format!("stripping a {kind_word} manually"),
|diag| {
diag.span_note(test_span, format!("the {kind_word} was tested here"));
multispan_sugg(
diag,
diag.multipart_suggestion(
format!("try using the `strip_{kind_word}` method"),
vec![(
iter::once((
test_span,
format!(
"if let Some(<stripped>) = {}.strip_{kind_word}({}) ",
snippet(cx, target_arg.span, ".."),
snippet(cx, pattern.span, "..")
),
)]
.into_iter()
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into()))),
))
.chain(strippings.into_iter().map(|span| (span, "<stripped>".into())))
.collect(),
Applicability::HasPlaceholders,
);
},
);
Expand Down Expand Up @@ -183,9 +185,9 @@ fn peel_ref<'a>(expr: &'a Expr<'_>) -> &'a Expr<'a> {
}
}

// Find expressions where `target` is stripped using the length of `pattern`.
// We'll suggest replacing these expressions with the result of the `strip_{prefix,suffix}`
// method.
/// Find expressions where `target` is stripped using the length of `pattern`.
/// We'll suggest replacing these expressions with the result of the `strip_{prefix,suffix}`
/// method.
fn find_stripping<'tcx>(
cx: &LateContext<'tcx>,
strip_kind: StripKind,
Expand Down
8 changes: 6 additions & 2 deletions clippy_lints/src/matches/match_ref_pats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::{snippet, walk_span_to_context};
use clippy_utils::sugg::Sugg;
use core::iter::once;
Expand Down Expand Up @@ -54,7 +54,11 @@ where

span_lint_and_then(cx, MATCH_REF_PATS, expr.span, title, |diag| {
if !expr.span.from_expansion() {
multispan_sugg(diag, msg, first_sugg.chain(remaining_suggs));
diag.multipart_suggestion(
msg,
first_sugg.chain(remaining_suggs).collect(),
Applicability::MachineApplicable,
);
}
});
}
Expand Down
19 changes: 10 additions & 9 deletions clippy_lints/src/methods/bind_instead_of_map.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{contains_return, BIND_INSTEAD_OF_MAP};
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::peel_blocks;
use clippy_utils::source::{snippet, snippet_with_context};
use clippy_utils::visitors::find_all_ret_expressions;
Expand Down Expand Up @@ -136,15 +136,16 @@ impl BindInsteadOfMap {
return false;
};
span_lint_and_then(cx, BIND_INSTEAD_OF_MAP, expr.span, msg, |diag| {
multispan_sugg_with_applicability(
diag,
"try",
diag.multipart_suggestion(
format!("use `{}` instead", self.good_method_name),
std::iter::once((span, self.good_method_name.into()))
.chain(
suggs
.into_iter()
.map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
)
.collect(),
Applicability::MachineApplicable,
std::iter::once((span, self.good_method_name.into())).chain(
suggs
.into_iter()
.map(|(span1, span2)| (span1, snippet(cx, span2, "_").into())),
),
);
});
true
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/methods/unused_enumerate_index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_hir_and_then};
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::{snippet, snippet_opt};
use clippy_utils::{expr_or_init, is_trait_method, pat_is_wild};
use rustc_errors::Applicability;
Expand Down Expand Up @@ -97,17 +97,16 @@ pub(super) fn check(cx: &LateContext<'_>, call_expr: &Expr<'_>, recv: &Expr<'_>,
enumerate_span,
"you seem to use `.enumerate()` and immediately discard the index",
|diag| {
multispan_sugg_with_applicability(
diag,
diag.multipart_suggestion(
"remove the `.enumerate()` call",
Applicability::MachineApplicable,
vec![
(closure_param.span, new_closure_param),
(
enumerate_span.with_lo(enumerate_recv.span.source_callsite().hi()),
String::new(),
),
],
Applicability::MachineApplicable,
);
},
);
Expand Down
11 changes: 7 additions & 4 deletions clippy_lints/src/needless_pass_by_value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_self;
use clippy_utils::ptr::get_spans;
use clippy_utils::source::{snippet, snippet_opt};
Expand Down Expand Up @@ -278,9 +278,12 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
}
}

let spans = vec![(input.span, format!("&{}", snippet(cx, input.span, "_")))];

multispan_sugg(diag, "consider taking a reference instead", spans);
diag.span_suggestion(
input.span,
"consider taking a reference instead",
format!("&{}", snippet(cx, input.span, "_")),
Applicability::MaybeIncorrect,
);
};

span_lint_and_then(
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/operators/op_ref.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::get_enclosing_block;
use clippy_utils::source::snippet;
use clippy_utils::ty::{implements_trait, is_copy};
Expand Down Expand Up @@ -64,10 +64,10 @@ pub(crate) fn check<'tcx>(
|diag| {
let lsnip = snippet(cx, l.span, "...").to_string();
let rsnip = snippet(cx, r.span, "...").to_string();
multispan_sugg(
diag,
diag.multipart_suggestion(
"use the values directly",
vec![(left.span, lsnip), (right.span, rsnip)],
Applicability::MachineApplicable,
);
},
);
Expand Down
12 changes: 5 additions & 7 deletions clippy_lints/src/semicolon_block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_config::Conf;
use clippy_utils::diagnostics::{multispan_sugg_with_applicability, span_lint_and_then};
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_errors::Applicability;
use rustc_hir::{Block, Expr, ExprKind, Stmt, StmtKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
Expand Down Expand Up @@ -92,11 +92,10 @@ impl SemicolonBlock {
semi_span,
"consider moving the `;` inside the block for consistent formatting",
|diag| {
multispan_sugg_with_applicability(
diag,
diag.multipart_suggestion(
"put the `;` here",
vec![(remove_span, String::new()), (insert_span, ";".to_owned())],
Applicability::MachineApplicable,
[(remove_span, String::new()), (insert_span, ";".to_owned())],
);
},
);
Expand Down Expand Up @@ -124,11 +123,10 @@ impl SemicolonBlock {
block.span,
"consider moving the `;` outside the block for consistent formatting",
|diag| {
multispan_sugg_with_applicability(
diag,
diag.multipart_suggestion(
"put the `;` here",
vec![(remove_span, String::new()), (insert_span, ";".to_owned())],
Applicability::MachineApplicable,
[(remove_span, String::new()), (insert_span, ";".to_owned())],
);
},
);
Expand Down
40 changes: 12 additions & 28 deletions clippy_lints/src/utils/internal_lints/metadata_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ const SUGGESTION_DIAG_METHODS: [(&str, bool); 9] = [
("tool_only_multipart_suggestion", true),
("span_suggestions", true),
];
const SUGGESTION_FUNCTIONS: [&[&str]; 2] = [
&["clippy_utils", "diagnostics", "multispan_sugg"],
&["clippy_utils", "diagnostics", "multispan_sugg_with_applicability"],
];
const DEPRECATED_LINT_TYPE: [&str; 3] = ["clippy_lints", "deprecated_lints", "ClippyDeprecatedLint"];

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

match &expr.kind {
ExprKind::Call(fn_expr, _args) => {
let found_function = SUGGESTION_FUNCTIONS
.iter()
.any(|func_path| match_function_call(self.cx, fn_expr, func_path).is_some());
if found_function {
// These functions are all multi part suggestions
self.add_single_span_suggestion();
}
},
ExprKind::MethodCall(path, recv, _, _arg_span) => {
let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(recv));
if match_type(self.cx, self_ty, &paths::DIAG) {
let called_method = path.ident.name.as_str().to_string();
for (method_name, is_multi_part) in &SUGGESTION_DIAG_METHODS {
if *method_name == called_method {
if *is_multi_part {
self.add_multi_part_suggestion();
} else {
self.add_single_span_suggestion();
}
break;
if let ExprKind::MethodCall(path, recv, _, _arg_span) = &expr.kind {
let (self_ty, _) = walk_ptrs_ty_depth(self.cx.typeck_results().expr_ty(recv));
if match_type(self.cx, self_ty, &paths::DIAG) {
let called_method = path.ident.name.as_str().to_string();
for (method_name, is_multi_part) in &SUGGESTION_DIAG_METHODS {
if *method_name == called_method {
if *is_multi_part {
self.add_multi_part_suggestion();
} else {
self.add_single_span_suggestion();
}
break;
}
}
},
_ => {},
}
}

intravisit::walk_expr(self, expr);
Expand Down
Loading

0 comments on commit 7ac242c

Please sign in to comment.