Rollup merge of #116787 - a-lafrance:span-internal-lint, r=oli-obk
Implement an internal lint encouraging use of `Span::eq_ctxt` Adds a new Rustc internal lint that forbids use of `.ctxt() == .ctxt()` for spans, encouraging use of `.eq_ctxt()` instead (see https://github.com/rust-lang/rust/issues/49509). Also fixed a few violations of the lint in the Rustc codebase (a fun additional way I could test my code). Edit: MIR opt folks I believe that's why you're CC'ed, just a heads up. Two things I'm not sure about: 1. The way I chose to detect calls to `Span::ctxt`. I know adding diagnostic items to methods is generally discouraged, but after some searching and experimenting I couldn't find anything else that worked, so I went with it. That said, I'm happy to implement something different if there's a better way out there. (For what it's worth, if there is a better way, it might be worth documenting in the rustc-dev-guide, which I'm happy to take care of) 2. The error message for the lint. Ideally it would probably be good to give some context as to why the suggestion is made (e.g. `rustc::default_hash_types` tells the user that it's because of performance), but I don't have that context so I couldn't put it in the error message. Happy to iterate on the error message based on feedback during review. r? ``@oli-obk``
This commit is contained in:
commit
3ea438eb3a
25 changed files with 89 additions and 21 deletions
|
|
@ -701,7 +701,7 @@ fn deref_method_same_type<'tcx>(result_ty: Ty<'tcx>, arg_ty: Ty<'tcx>) -> bool {
|
|||
|
||||
fn in_postfix_position<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'tcx>) -> bool {
|
||||
if let Some(parent) = get_parent_expr(cx, e)
|
||||
&& parent.span.ctxt() == e.span.ctxt()
|
||||
&& parent.span.eq_ctxt(e.span)
|
||||
{
|
||||
match parent.kind {
|
||||
ExprKind::Call(child, _) | ExprKind::MethodCall(_, child, _, _) | ExprKind::Index(child, _, _)
|
||||
|
|
|
|||
|
|
@ -241,7 +241,7 @@ fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Optio
|
|||
},
|
||||
],
|
||||
_,
|
||||
) if key_span.ctxt() == expr.span.ctxt() => {
|
||||
) if key_span.eq_ctxt(expr.span) => {
|
||||
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
|
||||
let expr = ContainsExpr {
|
||||
negated,
|
||||
|
|
|
|||
|
|
@ -274,7 +274,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
|
|||
for element in array {
|
||||
if_chain! {
|
||||
if let ExprKind::Binary(ref op, ref lhs, _) = element.kind;
|
||||
if has_unary_equivalent(op.node) && lhs.span.ctxt() == op.span.ctxt();
|
||||
if has_unary_equivalent(op.node) && lhs.span.eq_ctxt(op.span);
|
||||
let space_span = lhs.span.between(op.span);
|
||||
if let Some(space_snippet) = snippet_opt(cx, space_span);
|
||||
let lint_span = lhs.span.with_lo(lhs.span.hi());
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ impl LateLintPass<'_> for UnderscoreTyped {
|
|||
if !in_external_macro(cx.tcx.sess, local.span);
|
||||
if let Some(ty) = local.ty; // Ensure that it has a type defined
|
||||
if let TyKind::Infer = &ty.kind; // that type is '_'
|
||||
if local.span.ctxt() == ty.span.ctxt();
|
||||
if local.span.eq_ctxt(ty.span);
|
||||
then {
|
||||
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
|
||||
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@ impl<'tcx> QuestionMark {
|
|||
let Some(init) = local.init &&
|
||||
local.els.is_none() &&
|
||||
local.ty.is_none() &&
|
||||
init.span.ctxt() == stmt.span.ctxt() &&
|
||||
init.span.eq_ctxt(stmt.span) &&
|
||||
let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init)
|
||||
{
|
||||
match if_let_or_match {
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ fn check_arm<'tcx>(
|
|||
}
|
||||
},
|
||||
};
|
||||
if outer_pat.span.ctxt() == inner_scrutinee.span.ctxt();
|
||||
if outer_pat.span.eq_ctxt(inner_scrutinee.span);
|
||||
// match expression must be a local binding
|
||||
// match <local> { .. }
|
||||
if let Some(binding_id) = path_to_local(peel_ref_operators(cx, inner_scrutinee));
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ where
|
|||
// it's being passed by value.
|
||||
let scrutinee = peel_hir_expr_refs(scrutinee).0;
|
||||
let (scrutinee_str, _) = snippet_with_context(cx, scrutinee.span, expr_ctxt, "..", &mut app);
|
||||
let scrutinee_str = if scrutinee.span.ctxt() == expr.span.ctxt() && scrutinee.precedence().order() < PREC_POSTFIX {
|
||||
let scrutinee_str = if scrutinee.span.eq_ctxt(expr.span) && scrutinee.precedence().order() < PREC_POSTFIX {
|
||||
format!("({scrutinee_str})")
|
||||
} else {
|
||||
scrutinee_str.into()
|
||||
|
|
@ -130,7 +130,7 @@ where
|
|||
if_chain! {
|
||||
if !some_expr.needs_unsafe_block;
|
||||
if let Some(func) = can_pass_as_func(cx, id, some_expr.expr);
|
||||
if func.span.ctxt() == some_expr.expr.span.ctxt();
|
||||
if func.span.eq_ctxt(some_expr.expr.span);
|
||||
then {
|
||||
snippet_with_applicability(cx, func.span, "..", &mut app).into_owned()
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ pub(super) fn check<'tcx>(
|
|||
// lint, with note if neither arg is > 1 line and both map() and
|
||||
// unwrap_or_else() have the same span
|
||||
let multiline = map_snippet.lines().count() > 1 || unwrap_snippet.lines().count() > 1;
|
||||
let same_span = map_arg.span.ctxt() == unwrap_arg.span.ctxt();
|
||||
let same_span = map_arg.span.eq_ctxt(unwrap_arg.span);
|
||||
if same_span && !multiline {
|
||||
let var_snippet = snippet(cx, recv.span, "..");
|
||||
span_lint_and_sugg(
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ fn check(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||
if let ExprKind::Match(inner_expr_with_q, _, MatchSource::TryDesugar(_)) = &arg.kind;
|
||||
if let ExprKind::Call(called, [inner_expr]) = &inner_expr_with_q.kind;
|
||||
if let ExprKind::Path(QPath::LangItem(LangItem::TryTraitBranch, ..)) = &called.kind;
|
||||
if expr.span.ctxt() == inner_expr.span.ctxt();
|
||||
if expr.span.eq_ctxt(inner_expr.span);
|
||||
let expr_ty = cx.typeck_results().expr_ty(expr);
|
||||
let inner_ty = cx.typeck_results().expr_ty(inner_expr);
|
||||
if expr_ty == inner_ty;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
|
|||
|| (path.ident.name == sym!(set_mode)
|
||||
&& cx.tcx.is_diagnostic_item(sym::FsPermissions, adt.did()));
|
||||
if let ExprKind::Lit(_) = param.kind;
|
||||
if param.span.ctxt() == expr.span.ctxt();
|
||||
if param.span.eq_ctxt(expr.span);
|
||||
|
||||
then {
|
||||
let Some(snip) = snippet_opt(cx, param.span) else {
|
||||
|
|
@ -70,7 +70,7 @@ impl<'tcx> LateLintPass<'tcx> for NonOctalUnixPermissions {
|
|||
if let Some(def_id) = cx.qpath_res(path, func.hir_id).opt_def_id();
|
||||
if match_def_path(cx, def_id, &paths::PERMISSIONS_FROM_MODE);
|
||||
if let ExprKind::Lit(_) = param.kind;
|
||||
if param.span.ctxt() == expr.span.ctxt();
|
||||
if param.span.eq_ctxt(expr.span);
|
||||
if let Some(snip) = snippet_opt(cx, param.span);
|
||||
if !snip.starts_with("0o");
|
||||
then {
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ impl<'tcx> LateLintPass<'tcx> for RedundantAsyncBlock {
|
|||
let Some(body_expr) = desugar_async_block(cx, expr) &&
|
||||
let Some(expr) = desugar_await(peel_blocks(body_expr)) &&
|
||||
// The await prefix must not come from a macro as its content could change in the future.
|
||||
expr.span.ctxt() == body_expr.span.ctxt() &&
|
||||
expr.span.eq_ctxt(body_expr.span) &&
|
||||
// An async block does not have immediate side-effects from a `.await` point-of-view.
|
||||
(!expr.can_have_side_effects() || desugar_async_block(cx, expr).is_some()) &&
|
||||
let Some(shortened_span) = walk_span_to_context(expr.span, span.ctxt())
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ impl EarlyLintPass for DerefAddrOf {
|
|||
if_chain! {
|
||||
if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind;
|
||||
if let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind;
|
||||
if deref_target.span.ctxt() == e.span.ctxt();
|
||||
if deref_target.span.eq_ctxt(e.span);
|
||||
if !addrof_target.span.from_expansion();
|
||||
then {
|
||||
let mut applicability = Applicability::MachineApplicable;
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ impl LateLintPass<'_> for ConfusingXorAndPow {
|
|||
if !in_external_macro(cx.sess(), expr.span)
|
||||
&& let ExprKind::Binary(op, left, right) = &expr.kind
|
||||
&& op.node == BinOpKind::BitXor
|
||||
&& left.span.ctxt() == right.span.ctxt()
|
||||
&& left.span.eq_ctxt(right.span)
|
||||
&& let ExprKind::Lit(lit_left) = &left.kind
|
||||
&& let ExprKind::Lit(lit_right) = &right.kind
|
||||
&& matches!(lit_right.node, LitKind::Int(..) | LitKind::Float(..))
|
||||
|
|
|
|||
|
|
@ -245,7 +245,7 @@ impl<'a> PanicExpn<'a> {
|
|||
return None;
|
||||
};
|
||||
let result = match name {
|
||||
"panic" if arg.span.ctxt() == expr.span.ctxt() => Self::Empty,
|
||||
"panic" if arg.span.eq_ctxt(expr.span) => Self::Empty,
|
||||
"panic" | "panic_str" => Self::Str(arg),
|
||||
"panic_display" | "panic_cold_display" => {
|
||||
let ExprKind::AddrOf(_, _, e) = &arg.kind else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue