Skip to content

Commit 842f95d

Browse files
authored
Rollup merge of #147136 - Jules-Bertholet:const-_-unused-vis, r=jdonszelmann
Add warn-by-default lint for visibility on `const _` declarations Add a warn-by-default `unused_visibilities` lint for visibility qualifiers on `const _` declarations—e.g. `pub const _: () = ();`. Such qualifiers have no effect. A [Sourcegraph search](https://sourcegraph.com/search?q=context:global+lang:Rust+pub%5Cs*%28%5C%28.*%5C%29%29%3F%5Cs*const%5Cs%2B_%5Cs*:&patternType=regexp&case=yes&sm=0) suggests that this pattern is relatively rare, and mostly found in tests (with only 3 exceptions). So perhaps this could become an FCW/hard error in the future. `@rustbot` label T-lang A-lints A-visibility -T-clippy
2 parents d427ddf + 43fa060 commit 842f95d

File tree

17 files changed

+193
-14
lines changed

17 files changed

+193
-14
lines changed

compiler/rustc_ast_passes/src/ast_validation.rs

Lines changed: 14 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};
@@ -1339,14 +1339,26 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
13391339
}
13401340
});
13411341
}
1342-
ItemKind::Const(box ConstItem { defaultness, rhs, .. }) => {
1342+
ItemKind::Const(box ConstItem { defaultness, ident, rhs, .. }) => {
13431343
self.check_defaultness(item.span, *defaultness);
13441344
if rhs.is_none() {
13451345
self.dcx().emit_err(errors::ConstWithoutBody {
13461346
span: item.span,
13471347
replace_span: self.ending_semi_or_hi(item.span),
13481348
});
13491349
}
1350+
if ident.name == kw::Underscore
1351+
&& !matches!(item.vis.kind, VisibilityKind::Inherited)
1352+
&& ident.span.eq_ctxt(item.vis.span)
1353+
{
1354+
self.lint_buffer.buffer_lint(
1355+
UNUSED_VISIBILITIES,
1356+
item.id,
1357+
item.vis.span,
1358+
BuiltinLintDiag::UnusedVisibility(item.vis.span),
1359+
)
1360+
}
1361+
13501362
visit::walk_item(self, item);
13511363
}
13521364
ItemKind::Static(box StaticItem { expr, safety, .. }) => {

compiler/rustc_lint/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ lint_unused_op = unused {$op} that must be used
978978
979979
lint_unused_result = unused result of type `{$ty}`
980980
981+
lint_unused_visibilities = visibility qualifiers have no effect on `const _` declarations
982+
.note = `const _` does not declare a name, so there is nothing for the qualifier to apply to
983+
.suggestion = remove the qualifier
984+
981985
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
982986
983987
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
@@ -302,6 +302,9 @@ pub fn decorate_builtin_lint(
302302
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
303303
lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag)
304304
}
305+
BuiltinLintDiag::UnusedVisibility(span) => {
306+
lints::UnusedVisibility { span }.decorate_lint(diag)
307+
}
305308
BuiltinLintDiag::AttributeLint(kind) => decorate_attribute_lint(sess, tcx, &kind, diag),
306309
}
307310
}

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3191,3 +3191,11 @@ pub(crate) struct UnsafeAttrOutsideUnsafeSuggestion {
31913191
#[suggestion_part(code = ")")]
31923192
pub right: Span,
31933193
}
3194+
3195+
#[derive(LintDiagnostic)]
3196+
#[diag(lint_unused_visibilities)]
3197+
#[note]
3198+
pub(crate) struct UnusedVisibility {
3199+
#[suggestion(style = "short", code = "", applicability = "machine-applicable")]
3200+
pub span: Span,
3201+
}

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, as `const _` items are unnameable.
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
@@ -696,6 +696,7 @@ pub enum BuiltinLintDiag {
696696
extern_crate: Symbol,
697697
local_crate: Symbol,
698698
},
699+
UnusedVisibility(Span),
699700
AttributeLint(AttributeLintKind),
700701
}
701702

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)