Skip to content

Commit

Permalink
Make restriction lint's use span_lint_and_then (m -> m)
Browse files Browse the repository at this point in the history
  • Loading branch information
xFrednet committed Jul 22, 2024
1 parent bc8fc6b commit d17f113
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 47 deletions.
18 changes: 9 additions & 9 deletions clippy_lints/src/drop_forget_ref.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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);
}
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/float_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 5 additions & 4 deletions clippy_lints/src/inherent_impl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
},
);
}
}
Expand Down
13 changes: 9 additions & 4 deletions clippy_lints/src/methods/map_err_ignore.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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| ...`)",
);
},
);
}
}
10 changes: 6 additions & 4 deletions clippy_lints/src/missing_assert_message.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
},
);
}
}
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/missing_trait_methods.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
},
);
}
});
Expand Down
12 changes: 8 additions & 4 deletions clippy_lints/src/mixed_read_write_in_expression.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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",
);
},
);
}
}
Expand Down
32 changes: 17 additions & 15 deletions clippy_lints/src/module_style.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(),));
},
);
}
}
Expand Down Expand Up @@ -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()));
},
);
}
}

0 comments on commit d17f113

Please sign in to comment.