From 94a0819fe3a0d679c3042a77bfe6a2afc505daea Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:11:28 +0200 Subject: Adding upstream version 1.66.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_lint/src/internal.rs | 233 +++++++++++++++++++----------------- 1 file changed, 121 insertions(+), 112 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 c26d78247..11e4650cb 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -12,7 +12,6 @@ use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::hygiene::{ExpnKind, MacroKind}; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; -use tracing::debug; declare_tool_lint! { pub rustc::DEFAULT_HASH_TYPES, @@ -35,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) + }, + ); } } @@ -52,7 +54,7 @@ fn typeck_results_of_method_fn<'tcx>( expr: &Expr<'_>, ) -> Option<(Span, DefId, ty::subst::SubstsRef<'tcx>)> { match expr.kind { - ExprKind::MethodCall(segment, _, _) + ExprKind::MethodCall(segment, ..) if let Some(def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) => { Some((segment.ident.span, def_id, cx.typeck_results().node_substs(expr.hir_id))) @@ -81,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), + ) } } } @@ -119,21 +121,19 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind { _: rustc_hir::HirId, ) { if let Some(segment) = path.segments.iter().nth_back(1) - && let Some(res) = &segment.res - && lint_ty_kind_usage(cx, res) + && lint_ty_kind_usage(cx, &segment.res) { 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(); }); } } @@ -142,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(); }) } } @@ -246,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()) { @@ -310,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), ) } } @@ -351,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), + ); } } } @@ -374,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 } @@ -393,24 +390,33 @@ impl LateLintPass<'_> for Diagnostics { return; } + let mut found_parent_with_attr = false; let mut found_impl = false; - for (_, parent) in cx.tcx.hir().parent_iter(expr.hir_id) { + for (hir_id, parent) in cx.tcx.hir().parent_iter(expr.hir_id) { + if let Some(owner_did) = hir_id.as_owner() { + found_parent_with_attr = found_parent_with_attr + || cx.tcx.has_attr(owner_did.to_def_id(), sym::rustc_lint_diagnostics); + } + debug!(?parent); if let Node::Item(Item { kind: ItemKind::Impl(impl_), .. }) = parent && 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; } } debug!(?found_impl); - if !found_impl { - cx.struct_span_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, |lint| { - lint.build(fluent::lint::diag_out_of_impl).emit(); - }) + if !found_parent_with_attr && !found_impl { + cx.struct_span_lint( + DIAGNOSTIC_OUTSIDE_OF_IMPL, + span, + fluent::lint_diag_out_of_impl, + |lint| lint, + ) } let mut found_diagnostic_message = false; @@ -425,10 +431,13 @@ impl LateLintPass<'_> for Diagnostics { } } debug!(?found_diagnostic_message); - if !found_diagnostic_message { - cx.struct_span_lint(UNTRANSLATABLE_DIAGNOSTIC, span, |lint| { - lint.build(fluent::lint::untranslatable_diag).emit(); - }) + if !found_parent_with_attr && !found_diagnostic_message { + cx.struct_span_lint( + UNTRANSLATABLE_DIAGNOSTIC, + span, + fluent::lint_untranslatable_diag, + |lint| lint, + ) } } } @@ -460,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