Skip to content

Commit f05704d

Browse files
Add warn-by-default lint for visibility on const _ declarations
Add a warn-by-default `unused_visibility` lint for visibility qualifiers on `const _` declarations - e.g. `pub const _: () = ();`. These have no effect.
1 parent 568b117 commit f05704d

File tree

17 files changed

+141
-14
lines changed

17 files changed

+141
-14
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use rustc_session::Session;
3333
use rustc_session::lint::BuiltinLintDiag;
3434
use rustc_session::lint::builtin::{
3535
DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,
36-
PATTERNS_IN_FNS_WITHOUT_BODY,
36+
PATTERNS_IN_FNS_WITHOUT_BODY, UNUSED_VISIBILITIES,
3737
};
3838
use rustc_session::parse::feature_err;
3939
use rustc_span::{Ident, Span, kw, sym};
@@ -1331,14 +1331,25 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13311331
}
13321332
});
13331333
}
1334-
ItemKind::Const(box ConstItem { defaultness, rhs, .. }) => {
1334+
ItemKind::Const(box ConstItem { defaultness, ident, rhs, .. }) => {
13351335
self.check_defaultness(item.span, *defaultness);
13361336
if rhs.is_none() {
13371337
self.dcx().emit_err(errors::ConstWithoutBody {
13381338
span: item.span,
13391339
replace_span: self.ending_semi_or_hi(item.span),
13401340
});
13411341
}
1342+
if ident.name == kw::Underscore
1343+
&& !matches!(item.vis.kind, VisibilityKind::Inherited)
1344+
{
1345+
self.lint_buffer.buffer_lint(
1346+
UNUSED_VISIBILITIES,
1347+
item.id,
1348+
item.vis.span,
1349+
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1350+
)
1351+
}
1352+
13421353
visit::walk_item(self, item);
13431354
}
13441355
ItemKind::Static(box StaticItem { expr, safety, .. }) => {

compiler/rustc_lint/messages.ftl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,9 @@ lint_unused_op = unused {$op} that must be used
948948
949949
lint_unused_result = unused result of type `{$ty}`
950950
951+
lint_unused_visibilities = visibility qualifiers have no effect on `const _` declarations
952+
.suggestion = remove the qualifier
953+
951954
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
952955
953956
lint_useless_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,8 @@ pub fn decorate_builtin_lint(
322322
}
323323
.decorate_lint(diag)
324324
}
325+
BuiltinLintDiag::UnusedVisibility(span) => {
326+
lints::UnusedVisibility { span }.decorate_lint(diag)
327+
}
325328
}
326329
}

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ fn register_builtins(store: &mut LintStore) {
291291
"unused",
292292
UNUSED_IMPORTS,
293293
UNUSED_VARIABLES,
294+
UNUSED_VISIBILITIES,
294295
UNUSED_ASSIGNMENTS,
295296
DEAD_CODE,
296297
UNUSED_MUT,

compiler/rustc_lint/src/lints.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,3 +3147,10 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
31473147
}
31483148
}
31493149
}
3150+
3151+
#[derive(LintDiagnostic)]
3152+
#[diag(lint_unused_visibilities)]
3153+
pub(crate) struct UnusedVisibility {
3154+
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
3155+
pub span: Span,
3156+
}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ declare_lint_pass! {
143143
UNUSED_QUALIFICATIONS,
144144
UNUSED_UNSAFE,
145145
UNUSED_VARIABLES,
146+
UNUSED_VISIBILITIES,
146147
USELESS_DEPRECATED,
147148
VARARGS_WITHOUT_PATTERN,
148149
WARNINGS,
@@ -693,6 +694,26 @@ declare_lint! {
693694
"detect variables which are not used in any way"
694695
}
695696

697+
declare_lint! {
698+
/// The `unused_visibilities` lint detects visibility qualifiers (like `pub`)
699+
/// on a `const _` item.
700+
///
701+
/// ### Example
702+
///
703+
/// ```rust
704+
/// pub const _: () = {};
705+
/// ```
706+
///
707+
/// {{produces}}
708+
///
709+
/// ### Explanation
710+
///
711+
/// These qualifiers have no effect.
712+
pub UNUSED_VISIBILITIES,
713+
Warn,
714+
"detect visibility qualifiers on `const _` items"
715+
}
716+
696717
declare_lint! {
697718
/// The `unused_assignments` lint detects assignments that will never be read.
698719
///

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@ pub enum BuiltinLintDiag {
704704
suggestions: Vec<String>,
705705
docs: Option<&'static str>,
706706
},
707+
UnusedVisibility(Span),
707708
}
708709

709710
pub type RegisteredTools = FxIndexSet<Ident>;

src/tools/clippy/tests/ui-toml/absolute_paths/absolute_paths.no_short.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ LL | impl<T: core::cmp::Eq> core::fmt::Display for X<T>
7171
| ^^^^^^^^^^^^^^^^^^
7272

7373
error: consider bringing this path into scope with the `use` keyword
74-
--> tests/ui-toml/absolute_paths/absolute_paths.rs:113:14
74+
--> tests/ui-toml/absolute_paths/absolute_paths.rs:113:10
7575
|
76-
LL | pub const _: crate::S = {
77-
| ^^^^^^^^
76+
LL | const _: crate::S = {
77+
| ^^^^^^^^
7878

7979
error: consider bringing this path into scope with the `use` keyword
8080
--> tests/ui-toml/absolute_paths/absolute_paths.rs:114:9

src/tools/clippy/tests/ui-toml/absolute_paths/absolute_paths.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ mod m1 {
110110
}
111111

112112
//~[no_short]v absolute_paths
113-
pub const _: crate::S = {
113+
const _: crate::S = {
114114
let crate::S = m1::S; //~[no_short] absolute_paths
115115

116116
crate::m1::S

tests/ui/consts/promoted_const_call3.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ pub const C: () = {
44
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
55
};
66

7-
pub const _: () = {
7+
const _: () = {
88
let _: &'static _ = &id(&String::new());
99
//~^ ERROR: destructor of `String` cannot be evaluated at compile-time
1010
};
1111

12-
pub const _: () = {
12+
const _: () = {
1313
let _: &'static _ = &std::mem::ManuallyDrop::new(String::new());
1414
//~^ ERROR: temporary value dropped while borrowed
1515
};

0 commit comments

Comments
 (0)