Skip to content

Commit 0b19227

Browse files
committed
migrate: internal.rs
1 parent 5ffaae7 commit 0b19227

File tree

3 files changed

+99
-76
lines changed

3 files changed

+99
-76
lines changed

compiler/rustc_error_messages/locales/en-US/lint.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ lint_diag_out_of_impl =
8383
8484
lint_untranslatable_diag = diagnostics should be created using translatable messages
8585
86+
lint_bad_opt_access = {$msg}
87+
8688
lint_cstring_ptr = getting the inner pointer of a temporary `CString`
8789
.as_ptr_label = this pointer will be invalid
8890
.unwrap_label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime

compiler/rustc_lint/src/internal.rs

Lines changed: 35 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
//! Some lints that are only useful in the compiler or crates that use compiler internals, such as
22
//! Clippy.
33
4+
#![deny(rustc::untranslatable_diagnostic)]
5+
#![deny(rustc::diagnostic_outside_of_impl)]
6+
use crate::lints::{
7+
BadOptAccessDiag, DefaultHashTypesDiag, DiagOutOfImpl, LintPassByHand, NonExistantDocKeyword,
8+
QueryInstability, TyQualified, TykindDiag, TykindKind, UntranslatableDiag,
9+
};
410
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
511
use rustc_ast as ast;
6-
use rustc_errors::{fluent, Applicability};
712
use rustc_hir::def::Res;
813
use rustc_hir::{def_id::DefId, Expr, ExprKind, GenericArg, PatKind, Path, PathSegment, QPath};
914
use rustc_hir::{HirId, Impl, Item, ItemKind, Node, Pat, Ty, TyKind};
@@ -29,20 +34,15 @@ impl LateLintPass<'_> for DefaultHashTypes {
2934
// don't lint imports, only actual usages
3035
return;
3136
}
32-
let replace = match cx.tcx.get_diagnostic_name(def_id) {
37+
let preferred = match cx.tcx.get_diagnostic_name(def_id) {
3338
Some(sym::HashMap) => "FxHashMap",
3439
Some(sym::HashSet) => "FxHashSet",
3540
_ => return,
3641
};
37-
cx.struct_span_lint(
42+
cx.emit_spanned_lint(
3843
DEFAULT_HASH_TYPES,
3944
path.span,
40-
fluent::lint_default_hash_types,
41-
|lint| {
42-
lint.set_arg("preferred", replace)
43-
.set_arg("used", cx.tcx.item_name(def_id))
44-
.note(fluent::note)
45-
},
45+
DefaultHashTypesDiag { preferred, used: cx.tcx.item_name(def_id) },
4646
);
4747
}
4848
}
@@ -83,12 +83,11 @@ impl LateLintPass<'_> for QueryStability {
8383
if let Ok(Some(instance)) = ty::Instance::resolve(cx.tcx, cx.param_env, def_id, substs) {
8484
let def_id = instance.def_id();
8585
if cx.tcx.has_attr(def_id, sym::rustc_lint_query_instability) {
86-
cx.struct_span_lint(
86+
cx.emit_spanned_lint(
8787
POTENTIAL_QUERY_INSTABILITY,
8888
span,
89-
fluent::lint_query_instability,
90-
|lint| lint.set_arg("query", cx.tcx.item_name(def_id)).note(fluent::note),
91-
)
89+
QueryInstability { query: cx.tcx.item_name(def_id) },
90+
);
9291
}
9392
}
9493
}
@@ -126,14 +125,8 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
126125
let span = path.span.with_hi(
127126
segment.args.map_or(segment.ident.span, |a| a.span_ext).hi()
128127
);
129-
cx.struct_span_lint(USAGE_OF_TY_TYKIND, path.span, fluent::lint_tykind_kind, |lint| {
130-
lint
131-
.span_suggestion(
132-
span,
133-
fluent::suggestion,
134-
"ty",
135-
Applicability::MaybeIncorrect, // ty maybe needs an import
136-
)
128+
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
129+
suggestion: span,
137130
});
138131
}
139132
}
@@ -190,39 +183,17 @@ impl<'tcx> LateLintPass<'tcx> for TyTyKind {
190183

191184
match span {
192185
Some(span) => {
193-
cx.struct_span_lint(
194-
USAGE_OF_TY_TYKIND,
195-
path.span,
196-
fluent::lint_tykind_kind,
197-
|lint| lint.span_suggestion(
198-
span,
199-
fluent::suggestion,
200-
"ty",
201-
Applicability::MaybeIncorrect, // ty maybe needs an import
202-
)
203-
)
186+
cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindKind {
187+
suggestion: span,
188+
});
204189
},
205-
None => cx.struct_span_lint(
206-
USAGE_OF_TY_TYKIND,
207-
path.span,
208-
fluent::lint_tykind,
209-
|lint| lint.help(fluent::help)
210-
)
211-
}
212-
} else if !ty.span.from_expansion() && let Some(t) = is_ty_or_ty_ctxt(cx, &path) {
213-
if path.segments.len() > 1 {
214-
cx.struct_span_lint(USAGE_OF_QUALIFIED_TY, path.span, fluent::lint_ty_qualified, |lint| {
215-
lint
216-
.set_arg("ty", t.clone())
217-
.span_suggestion(
218-
path.span,
219-
fluent::suggestion,
220-
t,
221-
// The import probably needs to be changed
222-
Applicability::MaybeIncorrect,
223-
)
224-
})
190+
None => cx.emit_spanned_lint(USAGE_OF_TY_TYKIND, path.span, TykindDiag),
225191
}
192+
} else if !ty.span.from_expansion() && path.segments.len() > 1 && let Some(t) = is_ty_or_ty_ctxt(cx, &path) {
193+
cx.emit_spanned_lint(USAGE_OF_QUALIFIED_TY, path.span, TyQualified {
194+
ty: t.clone(),
195+
suggestion: path.span,
196+
});
226197
}
227198
}
228199
_ => {}
@@ -303,12 +274,11 @@ impl EarlyLintPass for LintPassImpl {
303274
&& call_site.ctxt().outer_expn_data().kind
304275
!= ExpnKind::Macro(MacroKind::Bang, sym::declare_lint_pass)
305276
{
306-
cx.struct_span_lint(
277+
cx.emit_spanned_lint(
307278
LINT_PASS_IMPL_WITHOUT_MACRO,
308279
lint_pass.path.span,
309-
fluent::lint_lintpass_by_hand,
310-
|lint| lint.help(fluent::help),
311-
)
280+
LintPassByHand,
281+
);
312282
}
313283
}
314284
}
@@ -338,17 +308,16 @@ impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
338308
if let Some(list) = attr.meta_item_list() {
339309
for nested in list {
340310
if nested.has_name(sym::keyword) {
341-
let v = nested
311+
let keyword = nested
342312
.value_str()
343313
.expect("#[doc(keyword = \"...\")] expected a value!");
344-
if is_doc_keyword(v) {
314+
if is_doc_keyword(keyword) {
345315
return;
346316
}
347-
cx.struct_span_lint(
317+
cx.emit_spanned_lint(
348318
EXISTING_DOC_KEYWORD,
349319
attr.span,
350-
fluent::lint_non_existant_doc_keyword,
351-
|lint| lint.set_arg("keyword", v).help(fluent::help),
320+
NonExistantDocKeyword { keyword },
352321
);
353322
}
354323
}
@@ -407,12 +376,7 @@ impl LateLintPass<'_> for Diagnostics {
407376
}
408377
debug!(?found_impl);
409378
if !found_parent_with_attr && !found_impl {
410-
cx.struct_span_lint(
411-
DIAGNOSTIC_OUTSIDE_OF_IMPL,
412-
span,
413-
fluent::lint_diag_out_of_impl,
414-
|lint| lint,
415-
)
379+
cx.emit_spanned_lint(DIAGNOSTIC_OUTSIDE_OF_IMPL, span, DiagOutOfImpl);
416380
}
417381

418382
let mut found_diagnostic_message = false;
@@ -428,12 +392,7 @@ impl LateLintPass<'_> for Diagnostics {
428392
}
429393
debug!(?found_diagnostic_message);
430394
if !found_parent_with_attr && !found_diagnostic_message {
431-
cx.struct_span_lint(
432-
UNTRANSLATABLE_DIAGNOSTIC,
433-
span,
434-
fluent::lint_untranslatable_diag,
435-
|lint| lint,
436-
)
395+
cx.emit_spanned_lint(UNTRANSLATABLE_DIAGNOSTIC, span, UntranslatableDiag);
437396
}
438397
}
439398
}
@@ -465,9 +424,9 @@ impl LateLintPass<'_> for BadOptAccess {
465424
let Some(lit) = item.lit() &&
466425
let ast::LitKind::Str(val, _) = lit.kind
467426
{
468-
cx.struct_span_lint(BAD_OPT_ACCESS, expr.span, val.as_str(), |lint|
469-
lint
470-
);
427+
cx.emit_spanned_lint(BAD_OPT_ACCESS, expr.span, BadOptAccessDiag {
428+
msg: val.as_str(),
429+
});
471430
}
472431
}
473432
}

compiler/rustc_lint/src/lints.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,68 @@ pub struct EnumIntrinsicsMemVariant<'a> {
4949
pub ty_param: Ty<'a>,
5050
}
5151

52+
// internal.rs
53+
#[derive(LintDiagnostic)]
54+
#[diag(lint_default_hash_types)]
55+
#[note]
56+
pub struct DefaultHashTypesDiag<'a> {
57+
pub preferred: &'a str,
58+
pub used: Symbol,
59+
}
60+
61+
#[derive(LintDiagnostic)]
62+
#[diag(lint_query_instability)]
63+
#[note]
64+
pub struct QueryInstability {
65+
pub query: Symbol,
66+
}
67+
68+
#[derive(LintDiagnostic)]
69+
#[diag(lint_tykind_kind)]
70+
pub struct TykindKind {
71+
#[suggestion(code = "ty", applicability = "maybe-incorrect")]
72+
pub suggestion: Span,
73+
}
74+
75+
#[derive(LintDiagnostic)]
76+
#[diag(lint_tykind)]
77+
#[help]
78+
pub struct TykindDiag;
79+
80+
#[derive(LintDiagnostic)]
81+
#[diag(lint_ty_qualified)]
82+
pub struct TyQualified {
83+
pub ty: String,
84+
#[suggestion(code = "{ty}", applicability = "maybe-incorrect")]
85+
pub suggestion: Span,
86+
}
87+
88+
#[derive(LintDiagnostic)]
89+
#[diag(lint_lintpass_by_hand)]
90+
#[help]
91+
pub struct LintPassByHand;
92+
93+
#[derive(LintDiagnostic)]
94+
#[diag(lint_non_existant_doc_keyword)]
95+
#[help]
96+
pub struct NonExistantDocKeyword {
97+
pub keyword: Symbol,
98+
}
99+
100+
#[derive(LintDiagnostic)]
101+
#[diag(lint_diag_out_of_impl)]
102+
pub struct DiagOutOfImpl;
103+
104+
#[derive(LintDiagnostic)]
105+
#[diag(lint_untranslatable_diag)]
106+
pub struct UntranslatableDiag;
107+
108+
#[derive(LintDiagnostic)]
109+
#[diag(lint_bad_opt_access)]
110+
pub struct BadOptAccessDiag<'a> {
111+
pub msg: &'a str,
112+
}
113+
52114
// let_underscore.rs
53115
#[derive(LintDiagnostic)]
54116
pub enum NonBindingLet {

0 commit comments

Comments
 (0)