Skip to content

Commit 1974186

Browse files
committed
migrate: rustc_lint::context
1 parent dbe8380 commit 1974186

File tree

3 files changed

+132
-59
lines changed

3 files changed

+132
-59
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,16 @@ lint_reason_must_come_last = reason in lint attribute must come last
414414
415415
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
416416
.help = add `#![register_tool({$tool_name})]` to the crate root
417+
418+
lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´
419+
420+
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
421+
422+
lint_check_name_unknown = unknown lint: `{$lint_name}`
423+
.help = did you mean: `{$suggestion}`
424+
425+
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
426+
427+
lint_check_name_warning = {$msg}
428+
429+
lint_check_name_deprecated = lint name `{$lint_name}` is deprecated and does not have an effect anymore. Use: {$new_name}

compiler/rustc_lint/src/context.rs

Lines changed: 38 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// #![deny(rustc::diagnostic_outside_of_impl)]
2+
// #![deny(rustc::untranslatable_diagnostic)]
3+
//
14
//! Implementation of lint checking.
25
//!
36
//! The lint checking is mostly consolidated into one pass which runs
@@ -16,12 +19,16 @@
1619
1720
use self::TargetLint::*;
1821

22+
use crate::errors::{
23+
CheckNameDeprecated, CheckNameUnknown, CheckNameUnknownTool, CheckNameWarning, RequestedLevel,
24+
UnsupportedGroup,
25+
};
1926
use crate::levels::LintLevelsBuilder;
2027
use crate::passes::{EarlyLintPassObject, LateLintPassObject};
2128
use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
2229
use rustc_data_structures::fx::FxHashMap;
2330
use rustc_data_structures::sync;
24-
use rustc_errors::{add_elided_lifetime_in_path_suggestion, struct_span_err};
31+
use rustc_errors::add_elided_lifetime_in_path_suggestion;
2532
use rustc_errors::{
2633
Applicability, DecorateLint, LintDiagnosticBuilder, MultiSpan, SuggestionStyle,
2734
};
@@ -39,7 +46,7 @@ use rustc_session::lint::{FutureIncompatibleInfo, Level, Lint, LintBuffer, LintI
3946
use rustc_session::Session;
4047
use rustc_span::lev_distance::find_best_match_for_name;
4148
use rustc_span::symbol::{sym, Ident, Symbol};
42-
use rustc_span::{BytePos, Span, DUMMY_SP};
49+
use rustc_span::{BytePos, Span};
4350
use rustc_target::abi;
4451
use tracing::debug;
4552

@@ -326,68 +333,41 @@ impl LintStore {
326333
) {
327334
let (tool_name, lint_name_only) = parse_lint_and_tool_name(lint_name);
328335
if lint_name_only == crate::WARNINGS.name_lower() && matches!(level, Level::ForceWarn(_)) {
329-
struct_span_err!(
330-
sess,
331-
DUMMY_SP,
332-
E0602,
333-
"`{}` lint group is not supported with ´--force-warn´",
334-
crate::WARNINGS.name_lower()
335-
)
336-
.emit();
336+
sess.emit_err(UnsupportedGroup { lint_group: crate::WARNINGS.name_lower() });
337337
return;
338338
}
339-
let db = match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
340-
CheckLintNameResult::Ok(_) => None,
341-
CheckLintNameResult::Warning(ref msg, _) => Some(sess.struct_warn(msg)),
339+
let lint_name = lint_name.to_string();
340+
match self.check_lint_name(lint_name_only, tool_name, registered_tools) {
341+
CheckLintNameResult::Warning(msg, _) => {
342+
sess.emit_warning(CheckNameWarning {
343+
msg,
344+
sub: RequestedLevel { level, lint_name },
345+
});
346+
}
342347
CheckLintNameResult::NoLint(suggestion) => {
343-
let mut err =
344-
struct_span_err!(sess, DUMMY_SP, E0602, "unknown lint: `{}`", lint_name);
345-
346-
if let Some(suggestion) = suggestion {
347-
err.help(&format!("did you mean: `{}`", suggestion));
348+
sess.emit_err(CheckNameUnknown {
349+
lint_name: lint_name.clone(),
350+
suggestion,
351+
sub: RequestedLevel { level, lint_name },
352+
});
353+
}
354+
CheckLintNameResult::Tool(result) => {
355+
if let Err((Some(_), new_name)) = result {
356+
sess.emit_warning(CheckNameDeprecated {
357+
lint_name: lint_name.clone(),
358+
new_name,
359+
sub: RequestedLevel { level, lint_name },
360+
});
348361
}
349-
350-
Some(err.forget_guarantee())
351362
}
352-
CheckLintNameResult::Tool(result) => match result {
353-
Err((Some(_), new_name)) => Some(sess.struct_warn(&format!(
354-
"lint name `{}` is deprecated \
355-
and does not have an effect anymore. \
356-
Use: {}",
357-
lint_name, new_name
358-
))),
359-
_ => None,
360-
},
361-
CheckLintNameResult::NoTool => Some(
362-
struct_span_err!(
363-
sess,
364-
DUMMY_SP,
365-
E0602,
366-
"unknown lint tool: `{}`",
367-
tool_name.unwrap()
368-
)
369-
.forget_guarantee(),
370-
),
363+
CheckLintNameResult::NoTool => {
364+
sess.emit_err(CheckNameUnknownTool {
365+
tool_name: tool_name.unwrap(),
366+
sub: RequestedLevel { level, lint_name },
367+
});
368+
}
369+
_ => {}
371370
};
372-
373-
if let Some(mut db) = db {
374-
let msg = format!(
375-
"requested on the command line with `{} {}`",
376-
match level {
377-
Level::Allow => "-A",
378-
Level::Warn => "-W",
379-
Level::ForceWarn(_) => "--force-warn",
380-
Level::Deny => "-D",
381-
Level::Forbid => "-F",
382-
Level::Expect(_) => {
383-
unreachable!("lints with the level of `expect` should not run this code");
384-
}
385-
},
386-
lint_name
387-
);
388-
db.note(&msg);
389-
db.emit();
390-
}
391371
}
392372

393373
/// True if this symbol represents a lint group name.

compiler/rustc_lint/src/errors.rs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use rustc_errors::{fluent, AddSubdiagnostic};
1+
use rustc_errors::{fluent, AddSubdiagnostic, ErrorGuaranteed};
22
use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic};
3+
use rustc_session::{lint::Level, parse::ParseSess, SessionDiagnostic};
34
use rustc_span::{Span, Symbol};
45

56
#[derive(SessionDiagnostic)]
@@ -80,3 +81,82 @@ pub struct BuiltinEllpisisInclusiveRangePatterns {
8081
pub suggestion: Span,
8182
pub replace: String,
8283
}
84+
85+
pub struct RequestedLevel {
86+
pub level: Level,
87+
pub lint_name: String,
88+
}
89+
90+
impl AddSubdiagnostic for RequestedLevel {
91+
fn add_to_diagnostic(self, diag: &mut rustc_errors::Diagnostic) {
92+
diag.note(fluent::lint::requested_level);
93+
diag.set_arg(
94+
"level",
95+
match self.level {
96+
Level::Allow => "-A",
97+
Level::Warn => "-W",
98+
Level::ForceWarn(_) => "--force-warn",
99+
Level::Deny => "-D",
100+
Level::Forbid => "-F",
101+
Level::Expect(_) => {
102+
unreachable!("lints with the level of `expect` should not run this code");
103+
}
104+
},
105+
);
106+
diag.set_arg("lint_name", self.lint_name);
107+
}
108+
}
109+
110+
#[derive(SessionDiagnostic)]
111+
#[error(lint::unsupported_group, code = "E0602")]
112+
pub struct UnsupportedGroup {
113+
pub lint_group: String,
114+
}
115+
116+
pub struct CheckNameUnknown {
117+
pub lint_name: String,
118+
pub suggestion: Option<Symbol>,
119+
pub sub: RequestedLevel,
120+
}
121+
122+
impl SessionDiagnostic<'_> for CheckNameUnknown {
123+
fn into_diagnostic(
124+
self,
125+
sess: &ParseSess,
126+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
127+
let mut diag = sess.struct_err(fluent::lint::check_name_unknown);
128+
diag.code(rustc_errors::error_code!(E0602));
129+
if let Some(suggestion) = self.suggestion {
130+
diag.help(fluent::lint::help);
131+
diag.set_arg("suggestion", suggestion);
132+
}
133+
diag.set_arg("lint_name", self.lint_name);
134+
diag.subdiagnostic(self.sub);
135+
diag
136+
}
137+
}
138+
139+
#[derive(SessionDiagnostic)]
140+
#[error(lint::check_name_unknown_tool, code = "E0602")]
141+
pub struct CheckNameUnknownTool {
142+
pub tool_name: Symbol,
143+
#[subdiagnostic]
144+
pub sub: RequestedLevel,
145+
}
146+
147+
#[derive(SessionDiagnostic)]
148+
#[warning(lint::check_name_warning)]
149+
pub struct CheckNameWarning {
150+
pub msg: String,
151+
#[subdiagnostic]
152+
pub sub: RequestedLevel,
153+
}
154+
155+
#[derive(SessionDiagnostic)]
156+
#[warning(lint::check_name_deprecated)]
157+
pub struct CheckNameDeprecated {
158+
pub lint_name: String,
159+
pub new_name: String,
160+
#[subdiagnostic]
161+
pub sub: RequestedLevel,
162+
}

0 commit comments

Comments
 (0)