Make restriction lint's use span_lint_and_then (t -> w)

This commit is contained in:
xFrednet 2024-07-22 20:59:01 +02:00
parent 7de9c20145
commit 4bf4c475ee
No known key found for this signature in database
GPG key ID: F5C59D0E669E5302
6 changed files with 92 additions and 72 deletions

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{is_refutable, peel_hir_pat_refs, recurse_or_patterns};
use rustc_errors::Applicability;
@ -148,23 +148,27 @@ pub(crate) fn check(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>]) {
Applicability::MaybeIncorrect,
),
variants => {
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
let message = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
suggestions.push("_".into());
"wildcard matches known variants and will also match future added variants"
let (message, add_wildcard) = if adt_def.is_variant_list_non_exhaustive() || has_external_hidden {
(
"wildcard matches known variants and will also match future added variants",
true,
)
} else {
"wildcard match will also match any future added variants"
("wildcard match will also match any future added variants", false)
};
span_lint_and_sugg(
cx,
WILDCARD_ENUM_MATCH_ARM,
wildcard_span,
message,
"try",
suggestions.join(" | "),
Applicability::MaybeIncorrect,
);
span_lint_and_then(cx, WILDCARD_ENUM_MATCH_ARM, wildcard_span, message, |diag| {
let mut suggestions: Vec<_> = variants.iter().copied().map(format_suggestion).collect();
if add_wildcard {
suggestions.push("_".into());
}
diag.span_suggestion(
wildcard_span,
"try",
suggestions.join(" | "),
Applicability::MaybeIncorrect,
);
});
},
};
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::{get_parent_expr, is_res_lang_ctor, path_res};
@ -48,29 +48,28 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, scrutine
return;
};
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
let mut applicability = Applicability::MachineApplicable;
let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
"" // already returns
} else {
"return "
};
let suggestion = if err_ty == expr_err_ty {
format!("{ret_prefix}{prefix}{origin_snippet}{suffix}")
} else {
format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}")
};
span_lint_and_sugg(
span_lint_and_then(
cx,
TRY_ERR,
expr.span,
"returning an `Err(_)` with the `?` operator",
"try",
suggestion,
applicability,
|diag| {
let expr_err_ty = cx.typeck_results().expr_ty(err_arg);
let span = hygiene::walk_chain(err_arg.span, try_arg.span.ctxt());
let mut applicability = Applicability::MachineApplicable;
let origin_snippet = snippet_with_applicability(cx, span, "_", &mut applicability);
let ret_prefix = if get_parent_expr(cx, expr).map_or(false, |e| matches!(e.kind, ExprKind::Ret(_))) {
"" // already returns
} else {
"return "
};
let suggestion = if err_ty == expr_err_ty {
format!("{ret_prefix}{prefix}{origin_snippet}{suffix}")
} else {
format!("{ret_prefix}{prefix}{origin_snippet}.into(){suffix}")
};
diag.span_suggestion(expr.span, "try", suggestion, applicability);
},
);
}
}

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_trait_method;
use clippy_utils::ty::is_type_diagnostic_item;
use rustc_hir::{Expr, ExprKind, QPath};
@ -23,6 +23,9 @@ pub(super) fn check<'tcx>(
&& matches!(recv.kind, ExprKind::Path(QPath::Resolved(None, _)))
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty_adjusted(recv).peel_refs(), sym::File)
{
span_lint_and_help(cx, VERBOSE_FILE_READS, expr.span, msg, None, help);
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(cx, VERBOSE_FILE_READS, expr.span, msg, |diag| {
diag.help(help);
});
}
}

View file

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::source::snippet_opt;
use rustc_ast::ast::{Pat, PatKind};
use rustc_lint::EarlyContext;
@ -21,13 +21,15 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
}
}
if !pfields.is_empty() && wilds == pfields.len() {
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
UNNEEDED_FIELD_PATTERN,
pat.span,
"all the struct fields are matched to a wildcard pattern, consider using `..`",
None,
format!("try with `{type_name} {{ .. }}` instead"),
|diag| {
diag.help(format!("try with `{type_name} {{ .. }}` instead"));
},
);
return;
}
@ -56,14 +58,15 @@ pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) {
}
}
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
UNNEEDED_FIELD_PATTERN,
field.span,
"you matched a field with a wildcard pattern, consider using `..` \
instead",
None,
format!("try with `{type_name} {{ {}, .. }}`", normal[..].join(", ")),
"you matched a field with a wildcard pattern, consider using `..` instead",
|diag| {
diag.help(format!("try with `{type_name} {{ {}, .. }}`", normal[..].join(", ")));
},
);
}
}

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_in_cfg_test, is_in_test_function};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Body, FnDecl};
@ -61,13 +61,15 @@ impl LateLintPass<'_> for TestsOutsideTestModule {
&& is_in_test_function(cx.tcx, body.id().hir_id)
&& !is_in_cfg_test(cx.tcx, body.id().hir_id)
{
span_lint_and_note(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
TESTS_OUTSIDE_TEST_MODULE,
sp,
"this function marked with #[test] is outside a #[cfg(test)] module",
None,
"move it to a testing module marked with #[cfg(test)]",
|diag| {
diag.note("move it to a testing module marked with #[cfg(test)]");
},
);
}
}

View file

@ -1,7 +1,7 @@
use std::ops::ControlFlow;
use clippy_config::Conf;
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::source::walk_span_to_context;
use clippy_utils::visitors::{for_each_expr, Descend};
@ -129,13 +129,15 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
block.span
};
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
UNDOCUMENTED_UNSAFE_BLOCKS,
span,
"unsafe block missing a safety comment",
None,
"consider adding a safety comment on the preceding line",
|diag| {
diag.help("consider adding a safety comment on the preceding line");
},
);
}
@ -145,13 +147,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, tail.span, tail.hir_id)
&& let Some(help_span) = expr_has_unnecessary_safety_comment(cx, tail, pos)
{
span_lint_and_help(
span_lint_and_then(
cx,
UNNECESSARY_SAFETY_COMMENT,
tail.span,
"expression has unnecessary safety comment",
Some(help_span),
"consider removing the safety comment",
|diag| {
diag.span_help(help_span, "consider removing the safety comment");
},
);
}
}
@ -168,13 +171,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id)
&& let Some(help_span) = expr_has_unnecessary_safety_comment(cx, expr, pos)
{
span_lint_and_help(
span_lint_and_then(
cx,
UNNECESSARY_SAFETY_COMMENT,
stmt.span,
"statement has unnecessary safety comment",
Some(help_span),
"consider removing the safety comment",
|diag| {
diag.span_help(help_span, "consider removing the safety comment");
},
);
}
}
@ -210,13 +214,15 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
item.span
};
span_lint_and_help(
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
span_lint_and_then(
cx,
UNDOCUMENTED_UNSAFE_BLOCKS,
span,
"unsafe impl missing a safety comment",
None,
"consider adding a safety comment on the preceding line",
|diag| {
diag.help("consider adding a safety comment on the preceding line");
},
);
}
},
@ -225,13 +231,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
let (span, help_span) = mk_spans(pos);
span_lint_and_help(
span_lint_and_then(
cx,
UNNECESSARY_SAFETY_COMMENT,
span,
"impl has unnecessary safety comment",
Some(help_span),
"consider removing the safety comment",
|diag| {
diag.span_help(help_span, "consider removing the safety comment");
},
);
}
},
@ -246,13 +253,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
) {
let (span, help_span) = mk_spans(pos);
span_lint_and_help(
span_lint_and_then(
cx,
UNNECESSARY_SAFETY_COMMENT,
span,
format!("{} has unnecessary safety comment", item.kind.descr()),
Some(help_span),
"consider removing the safety comment",
|diag| {
diag.span_help(help_span, "consider removing the safety comment");
},
);
}
}
@ -263,13 +271,14 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
let (span, help_span) = mk_spans(pos);
span_lint_and_help(
span_lint_and_then(
cx,
UNNECESSARY_SAFETY_COMMENT,
span,
format!("{} has unnecessary safety comment", item.kind.descr()),
Some(help_span),
"consider removing the safety comment",
|diag| {
diag.span_help(help_span, "consider removing the safety comment");
},
);
}
},