Skip to content

Commit fc19055

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 c8905ea commit fc19055

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_VISIBILITY,
3737
};
3838
use rustc_span::{Ident, Span, kw, sym};
3939
use rustc_target::spec::{AbiMap, AbiMapping};
@@ -1236,14 +1236,25 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
12361236
}
12371237
});
12381238
}
1239-
ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1239+
ItemKind::Const(box ConstItem { defaultness, ident, expr, .. }) => {
12401240
self.check_defaultness(item.span, *defaultness);
12411241
if expr.is_none() {
12421242
self.dcx().emit_err(errors::ConstWithoutBody {
12431243
span: item.span,
12441244
replace_span: self.ending_semi_or_hi(item.span),
12451245
});
12461246
}
1247+
if ident.name == kw::Underscore
1248+
&& !matches!(item.vis.kind, VisibilityKind::Inherited)
1249+
{
1250+
self.lint_buffer.buffer_lint(
1251+
UNUSED_VISIBILITY,
1252+
item.id,
1253+
item.vis.span,
1254+
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1255+
)
1256+
}
1257+
12471258
visit::walk_item(self, item);
12481259
}
12491260
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
@@ -956,6 +956,9 @@ lint_unused_op = unused {$op} that must be used
956956
957957
lint_unused_result = unused result of type `{$ty}`
958958
959+
lint_unused_visibility = visibility qualifiers have no effect on `const _` declarations
960+
.suggestion = remove the qualifier
961+
959962
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
960963
961964
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
@@ -288,6 +288,7 @@ fn register_builtins(store: &mut LintStore) {
288288
"unused",
289289
UNUSED_IMPORTS,
290290
UNUSED_VARIABLES,
291+
UNUSED_VISIBILITY,
291292
UNUSED_ASSIGNMENTS,
292293
DEAD_CODE,
293294
UNUSED_MUT,

compiler/rustc_lint/src/lints.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,3 +3188,10 @@ impl Subdiagnostic for MismatchedLifetimeSyntaxesSuggestion {
31883188
}
31893189
}
31903190
}
3191+
3192+
#[derive(LintDiagnostic)]
3193+
#[diag(lint_unused_visibility)]
3194+
pub(crate) struct UnusedVisibility {
3195+
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
3196+
pub span: Span,
3197+
}

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ declare_lint_pass! {
141141
UNUSED_QUALIFICATIONS,
142142
UNUSED_UNSAFE,
143143
UNUSED_VARIABLES,
144+
UNUSED_VISIBILITY,
144145
USELESS_DEPRECATED,
145146
WARNINGS,
146147
// tidy-alphabetical-end
@@ -690,6 +691,26 @@ declare_lint! {
690691
"detect variables which are not used in any way"
691692
}
692693

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

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)