From cf94bdc0742c13e2a0cac864c478b8626b266e1b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:11:38 +0200 Subject: Merging upstream version 1.66.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_lint/src/internal.rs | 215 ++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 105 deletions(-) (limited to 'compiler/rustc_lint/src/internal.rs') diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index dd1fc5916..11e4650cb 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -34,13 +34,16 @@ impl LateLintPass<'_> for DefaultHashTypes { Some(sym::HashSet) => "FxHashSet", _ => return, }; - cx.struct_span_lint(DEFAULT_HASH_TYPES, path.span, |lint| { - lint.build(fluent::lint::default_hash_types) - .set_arg("preferred", replace) - .set_arg("used", cx.tcx.item_name(def_id)) - .note(fluent::lint::note) - .emit(); - }); + cx.struct_span_lint( + DEFAULT_HASH_TYPES, + path.span, + fluent::lint_default_hash_types, + |lint| { + lint.set_arg("preferred", replace) + .set_arg("used", cx.tcx.item_name(def_id)) + .note(fluent::note) + }, + ); } } @@ -80,12 +83,12 @@ impl LateLintPass<'_> for QueryStability { if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) { let def_id = instance.def_id(); if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) { - cx.struct_span_lint(POTENTIAL_QUERY_INSTABILITY, span, |lint| { - lint.build(fluent::lint::query_instability) - .set_arg("query", cx.tcx.item_name(def_id)) - .note(fluent::lint::note) - .emit(); - }) + cx.struct_span_lint( + POTENTIAL_QUERY_INSTABILITY, + span, + fluent::lint_query_instability, + |lint| lint.set_arg("query", cx.tcx.item_name(def_id)).note(fluent::note), + ) } } } @@ -123,15 +126,14 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind { let span = path.span.with_hi( segment.args.map_or(segment.ident.span, |a| a.span_ext).hi() ); - cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| { - lint.build(fluent::lint::tykind_kind) + cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, fluent::lint_tykind_kind, |lint| { + lint .span_suggestion( span, - fluent::lint::suggestion, + fluent::suggestion, "ty", Applicability::MaybeIncorrect, // ty maybe needs an import ) - .emit(); }); } } @@ -140,85 +142,85 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind { match &ty.kind { TyKind::Path(QPath::Resolved(_, path)) => { if lint_ty_kind_usage(cx, &path.res) { - cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, |lint| { - let hir = cx.tcx.hir(); - match hir.find(hir.get_parent_node(ty.hir_id)) { - Some(Node::Pat(Pat { - kind: - PatKind::Path(qpath) - | PatKind::TupleStruct(qpath, ..) - | PatKind::Struct(qpath, ..), - .. - })) => { - if let QPath::TypeRelative(qpath_ty, ..) = qpath - && qpath_ty.hir_id == ty.hir_id - { - lint.build(fluent::lint::tykind_kind) - .span_suggestion( - path.span, - fluent::lint::suggestion, - "ty", - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - return; - } + let hir = cx.tcx.hir(); + let span = match hir.find(hir.get_parent_node(ty.hir_id)) { + Some(Node::Pat(Pat { + kind: + PatKind::Path(qpath) + | PatKind::TupleStruct(qpath, ..) + | PatKind::Struct(qpath, ..), + .. + })) => { + if let QPath::TypeRelative(qpath_ty, ..) = qpath + && qpath_ty.hir_id == ty.hir_id + { + Some(path.span) + } else { + None } - Some(Node::Expr(Expr { - kind: ExprKind::Path(qpath), - .. - })) => { - if let QPath::TypeRelative(qpath_ty, ..) = qpath - && qpath_ty.hir_id == ty.hir_id - { - lint.build(fluent::lint::tykind_kind) - .span_suggestion( - path.span, - fluent::lint::suggestion, - "ty", - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - return; - } + } + Some(Node::Expr(Expr { + kind: ExprKind::Path(qpath), + .. + })) => { + if let QPath::TypeRelative(qpath_ty, ..) = qpath + && qpath_ty.hir_id == ty.hir_id + { + Some(path.span) + } else { + None } - // Can't unify these two branches because qpath below is `&&` and above is `&` - // and `A | B` paths don't play well together with adjustments, apparently. - Some(Node::Expr(Expr { - kind: ExprKind::Struct(qpath, ..), - .. - })) => { - if let QPath::TypeRelative(qpath_ty, ..) = qpath - && qpath_ty.hir_id == ty.hir_id - { - lint.build(fluent::lint::tykind_kind) - .span_suggestion( - path.span, - fluent::lint::suggestion, - "ty", - Applicability::MaybeIncorrect, // ty maybe needs an import - ) - .emit(); - return; - } + } + // Can't unify these two branches because qpath below is `&&` and above is `&` + // and `A | B` paths don't play well together with adjustments, apparently. + Some(Node::Expr(Expr { + kind: ExprKind::Struct(qpath, ..), + .. + })) => { + if let QPath::TypeRelative(qpath_ty, ..) = qpath + && qpath_ty.hir_id == ty.hir_id + { + Some(path.span) + } else { + None } - _ => {} } - lint.build(fluent::lint::tykind).help(fluent::lint::help).emit(); - }) + _ => None + }; + + match span { + Some(span) => { + cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + fluent::lint_tykind_kind, + |lint| lint.span_suggestion( + span, + fluent::suggestion, + "ty", + Applicability::MaybeIncorrect, // ty maybe needs an import + ) + ) + }, + None => cx.struct_span_lint( + USAGE_OF_TY_TYKIND, + path.span, + fluent::lint_tykind, + |lint| lint.help(fluent::help) + ) + } } else if !ty.span.from_expansion() && let Some(t) = is_ty_or_ty_ctxt(cx, &path) { if path.segments.len() > 1 { - cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, |lint| { - lint.build(fluent::lint::ty_qualified) + cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, fluent::lint_ty_qualified, |lint| { + lint .set_arg("ty", t.clone()) .span_suggestion( path.span, - fluent::lint::suggestion, + fluent::suggestion, t, // The import probably needs to be changed Applicability::MaybeIncorrect, ) - .emit(); }) } } @@ -244,7 +246,7 @@ fn is_ty_or_ty_ctxt(cx: &LateContext<'_>, path: &Path<'_>) -> Option { } } // Only lint on `&Ty` and `&TyCtxt` if it is used outside of a trait. - Res::SelfTy { trait_: None, alias_to: Some((did, _)) } => { + Res::SelfTyAlias { alias_to: did, is_trait_impl: false, .. } => { if let ty::Adt(adt, substs) = cx.tcx.type_of(did).kind() { if let Some(name @ (sym::Ty | sym::TyCtxt)) = cx.tcx.get_diagnostic_name(adt.did()) { @@ -308,11 +310,8 @@ impl EarlyLintPass for LintPassImpl { cx.struct_span_lint( LINT_PASS_IMPL_WITHOUT_MACRO, lint_pass.path.span, - |lint| { - lint.build(fluent::lint::lintpass_by_hand) - .help(fluent::lint::help) - .emit(); - }, + fluent::lint_lintpass_by_hand, + |lint| lint.help(fluent::help), ) } } @@ -349,12 +348,12 @@ impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword { if is_doc_keyword(v) { return; } - cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| { - lint.build(fluent::lint::non_existant_doc_keyword) - .set_arg("keyword", v) - .help(fluent::lint::help) - .emit(); - }); + cx.struct_span_lint( + EXISTING_DOC_KEYWORD, + attr.span, + fluent::lint_non_existant_doc_keyword, + |lint| lint.set_arg("keyword", v).help(fluent::help), + ); } } } @@ -372,7 +371,7 @@ declare_tool_lint! { declare_tool_lint! { pub rustc::DIAGNOSTIC_OUTSIDE_OF_IMPL, Allow, - "prevent creation of diagnostics outside of `SessionDiagnostic`/`AddSubdiagnostic` impls", + "prevent creation of diagnostics outside of `IntoDiagnostic`/`AddToDiagnostic` impls", report_in_external_macro: true } @@ -404,7 +403,7 @@ impl LateLintPass<'_> for Diagnostics { let Impl { of_trait: Some(of_trait), .. } = impl_ && let Some(def_id) = of_trait.trait_def_id() && let Some(name) = cx.tcx.get_diagnostic_name(def_id) && - matches!(name, sym::SessionDiagnostic | sym::AddSubdiagnostic | sym::DecorateLint) + matches!(name, sym::IntoDiagnostic | sym::AddToDiagnostic | sym::DecorateLint) { found_impl = true; break; @@ -412,9 +411,12 @@ impl LateLintPass<'_> for Diagnostics { } debug!(?found_impl); if !found_parent_with_attr && !found_impl { - cx.struct_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, |lint| { - lint.build(fluent::lint::diag_out_of_impl).emit(); - }) + cx.struct_span_lint( + DIAGNOSTIC_OUTSIDE_OF_IMPL, + span, + fluent::lint_diag_out_of_impl, + |lint| lint, + ) } let mut found_diagnostic_message = false; @@ -430,9 +432,12 @@ impl LateLintPass<'_> for Diagnostics { } debug!(?found_diagnostic_message); if !found_parent_with_attr && !found_diagnostic_message { - cx.struct_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, |lint| { - lint.build(fluent::lint::untranslatable_diag).emit(); - }) + cx.struct_span_lint( + UNTRANSLATABLE_DIAGNOSTIC, + span, + fluent::lint_untranslatable_diag, + |lint| lint, + ) } } } @@ -464,8 +469,8 @@ impl LateLintPass<'_> for BadOptAccess { let Some(literal) = item.literal() && let ast::LitKind::Str(val, _) = literal.kind { - cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, |lint| { - lint.build(val.as_str()).emit(); } + cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, val.as_str(), |lint| + lint ); } } -- cgit v1.2.3