Skip to content

Commit 1ff0ba0

Browse files
authored
Rollup merge of #72970 - OddCoincidence:feature-gated-lints, r=petrochenkov
Properly handle feature-gated lints Closes #72694
2 parents cbab745 + e7e6bc1 commit 1ff0ba0

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

src/librustc_lint/levels.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ impl<'s> LintLevelsBuilder<'s> {
214214
match store.check_lint_name(&name.as_str(), tool_name) {
215215
CheckLintNameResult::Ok(ids) => {
216216
let src = LintSource::Node(name, li.span(), reason);
217-
for id in ids {
218-
self.check_gated_lint(*id, attr.span);
219-
specs.insert(*id, (level, src));
217+
for &id in ids {
218+
self.check_gated_lint(id, attr.span);
219+
specs.insert(id, (level, src));
220220
}
221221
}
222222

@@ -386,17 +386,18 @@ impl<'s> LintLevelsBuilder<'s> {
386386
BuilderPush { prev, changed: prev != self.cur }
387387
}
388388

389-
fn check_gated_lint(&self, id: LintId, span: Span) {
390-
if id == LintId::of(builtin::UNSAFE_OP_IN_UNSAFE_FN)
391-
&& !self.sess.features_untracked().unsafe_block_in_unsafe_fn
392-
{
393-
feature_err(
394-
&self.sess.parse_sess,
395-
sym::unsafe_block_in_unsafe_fn,
396-
span,
397-
"the `unsafe_op_in_unsafe_fn` lint is unstable",
398-
)
399-
.emit();
389+
/// Checks if the lint is gated on a feature that is not enabled.
390+
fn check_gated_lint(&self, lint_id: LintId, span: Span) {
391+
if let Some(feature) = lint_id.lint.feature_gate {
392+
if !self.sess.features_untracked().enabled(feature) {
393+
feature_err(
394+
&self.sess.parse_sess,
395+
feature,
396+
span,
397+
&format!("the `{}` lint is unstable", lint_id.lint.name_lower()),
398+
)
399+
.emit();
400+
}
400401
}
401402
}
402403

src/librustc_session/lint.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub struct Lint {
8585
pub future_incompatible: Option<FutureIncompatibleInfo>,
8686

8787
pub is_plugin: bool,
88+
89+
/// `Some` if this lint is feature gated, otherwise `None`.
90+
pub feature_gate: Option<Symbol>,
8891
}
8992

9093
/// Extra information for a future incompatibility lint.
@@ -107,6 +110,7 @@ impl Lint {
107110
is_plugin: false,
108111
report_in_external_macro: false,
109112
future_incompatible: None,
113+
feature_gate: None,
110114
}
111115
}
112116

@@ -276,7 +280,9 @@ macro_rules! declare_lint {
276280
);
277281
);
278282
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
279-
$(@future_incompatible = $fi:expr;)? $($v:ident),*) => (
283+
$(@future_incompatible = $fi:expr;)?
284+
$(@feature_gate = $gate:expr;)?
285+
$($v:ident),*) => (
280286
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
281287
name: stringify!($NAME),
282288
default_level: $crate::lint::$Level,
@@ -285,6 +291,7 @@ macro_rules! declare_lint {
285291
is_plugin: false,
286292
$($v: true,)*
287293
$(future_incompatible: Some($fi),)*
294+
$(feature_gate: Some($gate),)*
288295
..$crate::lint::Lint::default_fields_for_macro()
289296
};
290297
);
@@ -328,6 +335,7 @@ macro_rules! declare_tool_lint {
328335
report_in_external_macro: $external,
329336
future_incompatible: None,
330337
is_plugin: true,
338+
feature_gate: None,
331339
};
332340
);
333341
}

src/librustc_session/lint/builtin.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use crate::lint::FutureIncompatibleInfo;
88
use crate::{declare_lint, declare_lint_pass};
99
use rustc_span::edition::Edition;
10+
use rustc_span::symbol::sym;
1011

1112
declare_lint! {
1213
pub ILL_FORMED_ATTRIBUTE_INPUT,
@@ -530,6 +531,7 @@ declare_lint! {
530531
pub UNSAFE_OP_IN_UNSAFE_FN,
531532
Allow,
532533
"unsafe operations in unsafe functions without an explicit unsafe block are deprecated",
534+
@feature_gate = sym::unsafe_block_in_unsafe_fn;
533535
}
534536

535537
declare_lint_pass! {

src/librustdoc/core.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,6 @@ where
225225
{
226226
let warnings_lint_name = lint::builtin::WARNINGS.name;
227227

228-
// Whitelist feature-gated lints to avoid feature errors when trying to
229-
// allow all lints.
230-
// FIXME(#72694): handle feature-gated lints properly.
231-
let unsafe_op_in_unsafe_fn_name = rustc_lint::builtin::UNSAFE_OP_IN_UNSAFE_FN.name;
232-
233228
whitelisted_lints.push(warnings_lint_name.to_owned());
234229
whitelisted_lints.extend(lint_opts.iter().map(|(lint, _)| lint).cloned());
235230

@@ -241,7 +236,9 @@ where
241236

242237
let lint_opts = lints()
243238
.filter_map(|lint| {
244-
if lint.name == warnings_lint_name || lint.name == unsafe_op_in_unsafe_fn_name {
239+
// Whitelist feature-gated lints to avoid feature errors when trying to
240+
// allow all lints.
241+
if lint.name == warnings_lint_name || lint.feature_gate.is_some() {
245242
None
246243
} else {
247244
filter_call(lint)

0 commit comments

Comments
 (0)