Skip to content

Commit 15ef4ae

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 72b21e1 commit 15ef4ae

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_span::{Ident, Span, kw, sym};
3939
use rustc_target::spec::{AbiMap, AbiMapping};
@@ -1239,14 +1239,25 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12391239
}
12401240
});
12411241
}
1242-
ItemKind::Const(box ConstItem { defaultness, rhs, .. }) => {
1242+
ItemKind::Const(box ConstItem { defaultness, ident, rhs, .. }) => {
12431243
self.check_defaultness(item.span, *defaultness);
12441244
if rhs.is_none() {
12451245
self.dcx().emit_err(errors::ConstWithoutBody {
12461246
span: item.span,
12471247
replace_span: self.ending_semi_or_hi(item.span),
12481248
});
12491249
}
1250+
if ident.name == kw::Underscore
1251+
&& !matches!(item.vis.kind, VisibilityKind::Inherited)
1252+
{
1253+
self.lint_buffer.buffer_lint(
1254+
UNUSED_VISIBILITIES,
1255+
item.id,
1256+
item.vis.span,
1257+
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1258+
)
1259+
}
1260+
12501261
visit::walk_item(self, item);
12511262
}
12521263
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
@@ -959,6 +959,9 @@ lint_unused_op = unused {$op} that must be used
959959
960960
lint_unused_result = unused result of type `{$ty}`
961961
962+
lint_unused_visibilities = visibility qualifiers have no effect on `const _` declarations
963+
.suggestion = remove the qualifier
964+
962965
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
963966
964967
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
@@ -361,5 +361,8 @@ pub fn decorate_builtin_lint(
361361
BuiltinLintDiag::OutOfScopeMacroCalls { span, path, location } => {
362362
lints::OutOfScopeMacroCalls { span, path, location }.decorate_lint(diag)
363363
}
364+
BuiltinLintDiag::UnusedVisibility(span) => {
365+
lints::UnusedVisibility { span }.decorate_lint(diag)
366+
}
364367
}
365368
}

compiler/rustc_lint/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ fn register_builtins(store: &mut LintStore) {
285285
"unused",
286286
UNUSED_IMPORTS,
287287
UNUSED_VARIABLES,
288+
UNUSED_VISIBILITIES,
288289
UNUSED_ASSIGNMENTS,
289290
DEAD_CODE,
290291
UNUSED_MUT,

compiler/rustc_lint/src/lints.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3195,3 +3195,10 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
31953195
}
31963196
}
31973197
}
3198+
3199+
#[derive(LintDiagnostic)]
3200+
#[diag(lint_unused_visibilities)]
3201+
pub(crate) struct UnusedVisibility {
3202+
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
3203+
pub span: Span,
3204+
}

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
WARNINGS,
148149
// tidy-alphabetical-end
@@ -692,6 +693,26 @@ declare_lint! {
692693
"detect variables which are not used in any way"
693694
}
694695

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

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ pub enum BuiltinLintDiag {
748748
path: String,
749749
location: String,
750750
},
751+
UnusedVisibility(Span),
751752
}
752753

753754
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)