Make restriction lint's use span_lint_and_then (m -> m)

This commit is contained in:
xFrednet 2024-07-21 19:53:22 +02:00
parent bc8fc6b1d8
commit d17f113474
No known key found for this signature in database
GPG Key ID: F5C59D0E669E5302
8 changed files with 61 additions and 47 deletions

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_must_use_func_call;
use clippy_utils::ty::{is_copy, is_must_use_ty, is_type_lang_item};
use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
@ -126,14 +126,14 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
},
_ => return,
};
span_lint_and_note(
cx,
lint,
expr.span,
msg,
note_span,
format!("argument has type `{arg_ty}`"),
);
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
let note = format!("argument has type `{arg_ty}`");
if let Some(span) = note_span {
diag.span_note(span, note);
} else {
diag.note(note);
}
});
}
}
}

View File

@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for FloatLiteral {
// If the type suffix is missing the suggestion would be
// incorrectly interpreted as an integer so adding a `.0`
// suffix to prevent that.
span_lint_and_then(
cx,
LOSSY_FLOAT_LITERAL,

View File

@ -1,6 +1,6 @@
//! lint on inherent implementations
use clippy_utils::diagnostics::span_lint_and_note;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def_id::LocalDefId;
@ -106,13 +106,14 @@ impl<'tcx> LateLintPass<'tcx> for MultipleInherentImpl {
// `TyCtxt::crate_inherent_impls` doesn't have a defined order. Sort the lint output first.
lint_spans.sort_by_key(|x| x.0.lo());
for (span, first_span) in lint_spans {
span_lint_and_note(
span_lint_and_then(
cx,
MULTIPLE_INHERENT_IMPL,
span,
"multiple implementations of this structure",
Some(first_span),
"first implementation here",
|diag| {
diag.span_note(first_span, "first implementation here");
},
);
}
}

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_hir::{CaptureBy, Closure, Expr, ExprKind, PatKind};
use rustc_lint::LateContext;
@ -22,13 +22,18 @@ pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, arg: &Expr<'_>) {
{
// span the area of the closure capture and warn that the
// original error will be thrown away
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
MAP_ERR_IGNORE,
fn_decl_span,
"`map_err(|_|...` wildcard pattern discards the original error",
None,
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
|diag| {
diag.help(
"consider storing the original error as a source in the new error, or silence this warning using an ignored identifier (`.map_err(|_foo| ...`)",
);
},
);
}
}

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_in_test;
use clippy_utils::macros::{find_assert_args, find_assert_eq_args, root_macro_call_first_node, PanicExpn};
use rustc_hir::Expr;
@ -79,13 +79,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingAssertMessage {
};
if let PanicExpn::Empty = panic_expn {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
MISSING_ASSERT_MESSAGE,
macro_call.span,
"assert without any message",
None,
"consider describing why the failing assert is problematic",
|diag| {
diag.help("consider describing why the failing assert is problematic");
},
);
}
}

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::is_lint_allowed;
use clippy_utils::macros::span_is_local;
use rustc_hir::def_id::DefIdMap;
@ -83,15 +83,15 @@ impl<'tcx> LateLintPass<'tcx> for MissingTraitMethods {
cx.tcx.with_stable_hashing_context(|hcx| {
for assoc in provided.values_sorted(&hcx, true) {
let source_map = cx.tcx.sess.source_map();
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
span_lint_and_help(
span_lint_and_then(
cx,
MISSING_TRAIT_METHODS,
source_map.guess_head_span(item.span),
format!("missing trait method provided by default: `{}`", assoc.name),
Some(definition_span),
"implement the method",
|diag| {
let definition_span = source_map.guess_head_span(cx.tcx.def_span(assoc.def_id));
diag.span_help(definition_span, "implement the method");
},
);
}
});

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_note};
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::{get_parent_expr, path_to_local, path_to_local_id};
use rustc_hir::intravisit::{walk_expr, Visitor};
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, HirId, LetStmt, Node, Stmt, StmtKind};
@ -324,13 +324,17 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
if path_to_local_id(expr, self.var) {
// Check that this is a read, not a write.
if !is_in_assignment_position(self.cx, expr) {
span_lint_and_note(
span_lint_and_then(
self.cx,
MIXED_READ_WRITE_IN_EXPRESSION,
expr.span,
format!("unsequenced read of `{}`", self.cx.tcx.hir().name(self.var)),
Some(self.write_expr.span),
"whether read occurs before this write depends on evaluation order",
|diag| {
diag.span_note(
self.write_expr.span,
"whether read occurs before this write depends on evaluation order",
);
},
);
}
}

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_ast::ast;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_lint::{EarlyContext, EarlyLintPass, Level, LintContext};
@ -121,17 +121,18 @@ impl EarlyLintPass for ModStyle {
for folder in &folder_segments {
if !mod_folders.contains(folder) {
if let Some((file, path)) = file_map.get(folder) {
let mut correct = path.to_path_buf();
correct.pop();
correct.push(folder);
correct.push("mod.rs");
span_lint_and_help(
span_lint_and_then(
cx,
SELF_NAMED_MODULE_FILES,
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
format!("`mod.rs` files are required, found `{}`", path.display()),
None,
format!("move `{}` to `{}`", path.display(), correct.display(),),
|diag| {
let mut correct = path.to_path_buf();
correct.pop();
correct.push(folder);
correct.push("mod.rs");
diag.help(format!("move `{}` to `{}`", path.display(), correct.display(),));
},
);
}
}
@ -161,17 +162,18 @@ fn process_paths_for_mod_files<'a>(
/// for code-sharing between tests.
fn check_self_named_mod_exists(cx: &EarlyContext<'_>, path: &Path, file: &SourceFile) {
if path.ends_with("mod.rs") && !path.starts_with("tests") {
let mut mod_file = path.to_path_buf();
mod_file.pop();
mod_file.set_extension("rs");
span_lint_and_help(
span_lint_and_then(
cx,
MOD_MODULE_FILES,
Span::new(file.start_pos, file.start_pos, SyntaxContext::root(), None),
format!("`mod.rs` files are not allowed, found `{}`", path.display()),
None,
format!("move `{}` to `{}`", path.display(), mod_file.display()),
|diag| {
let mut mod_file = path.to_path_buf();
mod_file.pop();
mod_file.set_extension("rs");
diag.help(format!("move `{}` to `{}`", path.display(), mod_file.display()));
},
);
}
}