migrate: internal.rs

This commit is contained in:
Rejyr 2022-10-22 16:32:54 -04:00
parent 5ffaae758e
commit 0b19227524
3 changed files with 99 additions and 76 deletions

View file

@ -83,6 +83,8 @@ lint_diag_out_of_impl =
lint_untranslatable_diag = diagnostics should be created using translatable messages
lint_bad_opt_access = {$msg}
lint_cstring_ptr = getting the inner pointer of a temporary `CString`
.as_ptr_label = this pointer will be invalid
.unwrap_label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime

View file

@ -1,9 +1,14 @@
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
//! Clippy.
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
use crate::lints::{
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistantDocKeyword,
QueryInstability, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
};
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
use rustc_ast as ast;
use rustc_errors::{fluent, Applicability};
use rustc_hir::def::Res;
use rustc_hir::{def_id::DefId, Expr, ExprKind, GenericArg, PatKind, Path, PathSegment, QPath};
use rustc_hir::{HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
@ -29,20 +34,15 @@ impl LateLintPass<'_> for DefaultHashTypes {
// don't lint imports, only actual usages
return;
}
let replace = match cx.tcx.get_diagnostic_name(def_id) {
let preferred = match cx.tcx.get_diagnostic_name(def_id) {
Some(sym::HashMap) => "FxHashMap",
Some(sym::HashSet) => "FxHashSet",
_ => return,
};
cx.struct_span_lint(
cx.emit_spanned_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)
},
DefaultHashTypesDiag { preferred, used: cx.tcx.item_name(def_id) },
);
}
}
@ -83,12 +83,11 @@ 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(
cx.emit_spanned_lint(
POTENTIAL_QUERY_INSTABILITY,
span,
fluent::lint_query_instability,
|lint| lint.set_arg("query", cx.tcx.item_name(def_id)).note(fluent::note),
)
QueryInstability { query: cx.tcx.item_name(def_id) },
);
}
}
}
@ -126,14 +125,8 @@ 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, fluent::lint_tykind_kind, |lint| {
lint
.span_suggestion(
span,
fluent::suggestion,
"ty",
Applicability::MaybeIncorrect, // ty maybe needs an import
)
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
suggestion: span,
});
}
}
@ -190,39 +183,17 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
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
)
)
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
suggestion: span,
});
},
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, fluent::lint_ty_qualified, |lint| {
lint
.set_arg("ty", t.clone())
.span_suggestion(
path.span,
fluent::suggestion,
t,
// The import probably needs to be changed
Applicability::MaybeIncorrect,
)
})
None => cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindDiag),
}
} else if !ty.span.from_expansion() && path.segments.len() > 1 && let Some(t) = is_ty_or_ty_ctxt(cx, &path) {
cx.emit_spanned_lint(USAGE_OF_QUALIFIED_TY, path.span, TyQualified {
ty: t.clone(),
suggestion: path.span,
});
}
}
_ => {}
@ -303,12 +274,11 @@ impl EarlyLintPass for LintPassImpl {
&& call_site.ctxt().outer_expn_data().kind
!= ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
{
cx.struct_span_lint(
cx.emit_spanned_lint(
LINT_PASS_IMPL_WITHOUT_MACRO,
lint_pass.path.span,
fluent::lint_lintpass_by_hand,
|lint| lint.help(fluent::help),
)
LintPassByHand,
);
}
}
}
@ -338,17 +308,16 @@ impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
if let Some(list) = attr.meta_item_list() {
for nested in list {
if nested.has_name(sym::keyword) {
let v = nested
let keyword = nested
.value_str()
.expect("#[doc(keyword = \"...\")] expected a value!");
if is_doc_keyword(v) {
if is_doc_keyword(keyword) {
return;
}
cx.struct_span_lint(
cx.emit_spanned_lint(
EXISTING_DOC_KEYWORD,
attr.span,
fluent::lint_non_existant_doc_keyword,
|lint| lint.set_arg("keyword", v).help(fluent::help),
NonExistantDocKeyword { keyword },
);
}
}
@ -407,12 +376,7 @@ impl LateLintPass<'_> for Diagnostics {
}
debug!(?found_impl);
if !found_parent_with_attr && !found_impl {
cx.struct_span_lint(
DIAGNOSTIC_OUTSIDE_OF_IMPL,
span,
fluent::lint_diag_out_of_impl,
|lint| lint,
)
cx.emit_spanned_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, DiagOutOfImpl);
}
let mut found_diagnostic_message = false;
@ -428,12 +392,7 @@ impl LateLintPass<'_> for Diagnostics {
}
debug!(?found_diagnostic_message);
if !found_parent_with_attr && !found_diagnostic_message {
cx.struct_span_lint(
UNTRANSLATABLE_DIAGNOSTIC,
span,
fluent::lint_untranslatable_diag,
|lint| lint,
)
cx.emit_spanned_lint(UNTRANSLATABLE_DIAGNOSTIC, span, UntranslatableDiag);
}
}
}
@ -465,9 +424,9 @@ impl LateLintPass<'_> for BadOptAccess {
let Some(lit) = item.lit() &&
let ast::LitKind::Str(val, _) = lit.kind
{
cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, val.as_str(), |lint|
lint
);
cx.emit_spanned_lint(BAD_OPT_ACCESS, expr.span, BadOptAccessDiag {
msg: val.as_str(),
});
}
}
}

View file

@ -49,6 +49,68 @@ pub struct EnumIntrinsicsMemVariant<'a> {
pub ty_param: Ty<'a>,
}
// internal.rs
#[derive(LintDiagnostic)]
#[diag(lint_default_hash_types)]
#[note]
pub struct DefaultHashTypesDiag<'a> {
pub preferred: &'a str,
pub used: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_query_instability)]
#[note]
pub struct QueryInstability {
pub query: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_tykind_kind)]
pub struct TykindKind {
#[suggestion(code = "ty", applicability = "maybe-incorrect")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_tykind)]
#[help]
pub struct TykindDiag;
#[derive(LintDiagnostic)]
#[diag(lint_ty_qualified)]
pub struct TyQualified {
pub ty: String,
#[suggestion(code = "{ty}", applicability = "maybe-incorrect")]
pub suggestion: Span,
}
#[derive(LintDiagnostic)]
#[diag(lint_lintpass_by_hand)]
#[help]
pub struct LintPassByHand;
#[derive(LintDiagnostic)]
#[diag(lint_non_existant_doc_keyword)]
#[help]
pub struct NonExistantDocKeyword {
pub keyword: Symbol,
}
#[derive(LintDiagnostic)]
#[diag(lint_diag_out_of_impl)]
pub struct DiagOutOfImpl;
#[derive(LintDiagnostic)]
#[diag(lint_untranslatable_diag)]
pub struct UntranslatableDiag;
#[derive(LintDiagnostic)]
#[diag(lint_bad_opt_access)]
pub struct BadOptAccessDiag<'a> {
pub msg: &'a str,
}
// let_underscore.rs
#[derive(LintDiagnostic)]
pub enum NonBindingLet {