Skip to content

Commit f91f369

Browse files
authored
Rollup merge of #106148 - chenyukang:yukang/fix-105061-unused, r=lcnr
Fix unused_parens issue for higher ranked function pointers fixes #105061 r? `@lcnr`
2 parents 09faa26 + 9d74bb8 commit f91f369

11 files changed

+224
-28
lines changed

compiler/rustc_lint/src/early.rs

+2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,9 @@ impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T>
248248
}
249249

250250
fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
251+
lint_callback!(self, enter_where_predicate, p);
251252
ast_visit::walk_where_predicate(self, p);
253+
lint_callback!(self, exit_where_predicate, p);
252254
}
253255

254256
fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {

compiler/rustc_lint/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ early_lint_methods!(
145145
[
146146
pub BuiltinCombinedEarlyLintPass,
147147
[
148-
UnusedParens: UnusedParens,
148+
UnusedParens: UnusedParens::new(),
149149
UnusedBraces: UnusedBraces,
150150
UnusedImportBraces: UnusedImportBraces,
151151
UnsafeCode: UnsafeCode,

compiler/rustc_lint/src/passes.rs

+3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ macro_rules! early_lint_methods {
171171

172172
/// Counterpart to `enter_lint_attrs`.
173173
fn exit_lint_attrs(a: &[ast::Attribute]);
174+
175+
fn enter_where_predicate(a: &ast::WherePredicate);
176+
fn exit_where_predicate(a: &ast::WherePredicate);
174177
]);
175178
)
176179
}

compiler/rustc_lint/src/unused.rs

+55-23
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,17 @@ declare_lint! {
824824
"`if`, `match`, `while` and `return` do not need parentheses"
825825
}
826826

827-
declare_lint_pass!(UnusedParens => [UNUSED_PARENS]);
827+
pub struct UnusedParens {
828+
with_self_ty_parens: bool,
829+
}
830+
831+
impl UnusedParens {
832+
pub fn new() -> Self {
833+
Self { with_self_ty_parens: false }
834+
}
835+
}
836+
837+
impl_lint_pass!(UnusedParens => [UNUSED_PARENS]);
828838

829839
impl UnusedDelimLint for UnusedParens {
830840
const DELIM_STR: &'static str = "parentheses";
@@ -999,36 +1009,58 @@ impl EarlyLintPass for UnusedParens {
9991009
}
10001010

10011011
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
1002-
if let ast::TyKind::Paren(r) = &ty.kind {
1003-
match &r.kind {
1004-
ast::TyKind::TraitObject(..) => {}
1005-
ast::TyKind::BareFn(b) if b.generic_params.len() > 0 => {}
1006-
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
1007-
ast::TyKind::Array(_, len) => {
1008-
self.check_unused_delims_expr(
1009-
cx,
1010-
&len.value,
1011-
UnusedDelimsCtx::ArrayLenExpr,
1012-
false,
1013-
None,
1014-
None,
1015-
);
1016-
}
1017-
_ => {
1018-
let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1019-
Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1020-
} else {
1021-
None
1022-
};
1023-
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1012+
match &ty.kind {
1013+
ast::TyKind::Array(_, len) => {
1014+
self.check_unused_delims_expr(
1015+
cx,
1016+
&len.value,
1017+
UnusedDelimsCtx::ArrayLenExpr,
1018+
false,
1019+
None,
1020+
None,
1021+
);
1022+
}
1023+
ast::TyKind::Paren(r) => {
1024+
match &r.kind {
1025+
ast::TyKind::TraitObject(..) => {}
1026+
ast::TyKind::BareFn(b)
1027+
if self.with_self_ty_parens && b.generic_params.len() > 0 => {}
1028+
ast::TyKind::ImplTrait(_, bounds) if bounds.len() > 1 => {}
1029+
_ => {
1030+
let spans = if let Some(r) = r.span.find_ancestor_inside(ty.span) {
1031+
Some((ty.span.with_hi(r.lo()), ty.span.with_lo(r.hi())))
1032+
} else {
1033+
None
1034+
};
1035+
self.emit_unused_delims(cx, ty.span, spans, "type", (false, false));
1036+
}
10241037
}
1038+
self.with_self_ty_parens = false;
10251039
}
1040+
_ => {}
10261041
}
10271042
}
10281043

10291044
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &ast::Item) {
10301045
<Self as UnusedDelimLint>::check_item(self, cx, item)
10311046
}
1047+
1048+
fn enter_where_predicate(&mut self, _: &EarlyContext<'_>, pred: &ast::WherePredicate) {
1049+
use rustc_ast::{WhereBoundPredicate, WherePredicate};
1050+
if let WherePredicate::BoundPredicate(WhereBoundPredicate {
1051+
bounded_ty,
1052+
bound_generic_params,
1053+
..
1054+
}) = pred &&
1055+
let ast::TyKind::Paren(_) = &bounded_ty.kind &&
1056+
bound_generic_params.is_empty() {
1057+
self.with_self_ty_parens = true;
1058+
}
1059+
}
1060+
1061+
fn exit_where_predicate(&mut self, _: &EarlyContext<'_>, _: &ast::WherePredicate) {
1062+
assert!(!self.with_self_ty_parens);
1063+
}
10321064
}
10331065

10341066
declare_lint! {

library/std/src/io/error/repr_bitpacked.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ static_assert!((TAG_MASK + 1).is_power_of_two());
374374
static_assert!(align_of::<SimpleMessage>() >= TAG_MASK + 1);
375375
static_assert!(align_of::<Custom>() >= TAG_MASK + 1);
376376

377-
static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE_MESSAGE), TAG_SIMPLE_MESSAGE);
378-
static_assert!(@usize_eq: (TAG_MASK & TAG_CUSTOM), TAG_CUSTOM);
379-
static_assert!(@usize_eq: (TAG_MASK & TAG_OS), TAG_OS);
380-
static_assert!(@usize_eq: (TAG_MASK & TAG_SIMPLE), TAG_SIMPLE);
377+
static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE_MESSAGE, TAG_SIMPLE_MESSAGE);
378+
static_assert!(@usize_eq: TAG_MASK & TAG_CUSTOM, TAG_CUSTOM);
379+
static_assert!(@usize_eq: TAG_MASK & TAG_OS, TAG_OS);
380+
static_assert!(@usize_eq: TAG_MASK & TAG_SIMPLE, TAG_SIMPLE);
381381

382382
// This is obviously true (`TAG_CUSTOM` is `0b01`), but in `Repr::new_custom` we
383383
// offset a pointer by this value, and expect it to both be within the same
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![warn(unused)]
2+
#![deny(warnings)]
3+
4+
fn main() {
5+
let _x: ([u32; 3]); //~ ERROR unnecessary parentheses around type
6+
let _y: [u8; (3)]; //~ ERROR unnecessary parentheses around const expression
7+
let _z: ([u8; (3)]);
8+
//~^ ERROR unnecessary parentheses around const expression
9+
//~| ERROR unnecessary parentheses around type
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error: unnecessary parentheses around type
2+
--> $DIR/issue-105061-array-lint.rs:5:13
3+
|
4+
LL | let _x: ([u32; 3]);
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-105061-array-lint.rs:2:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unused_parens)]` implied by `#[deny(warnings)]`
13+
help: remove these parentheses
14+
|
15+
LL - let _x: ([u32; 3]);
16+
LL + let _x: [u32; 3];
17+
|
18+
19+
error: unnecessary parentheses around const expression
20+
--> $DIR/issue-105061-array-lint.rs:6:18
21+
|
22+
LL | let _y: [u8; (3)];
23+
| ^ ^
24+
|
25+
help: remove these parentheses
26+
|
27+
LL - let _y: [u8; (3)];
28+
LL + let _y: [u8; 3];
29+
|
30+
31+
error: unnecessary parentheses around type
32+
--> $DIR/issue-105061-array-lint.rs:7:13
33+
|
34+
LL | let _z: ([u8; (3)]);
35+
| ^ ^
36+
|
37+
help: remove these parentheses
38+
|
39+
LL - let _z: ([u8; (3)]);
40+
LL + let _z: [u8; (3)];
41+
|
42+
43+
error: unnecessary parentheses around const expression
44+
--> $DIR/issue-105061-array-lint.rs:7:19
45+
|
46+
LL | let _z: ([u8; (3)]);
47+
| ^ ^
48+
|
49+
help: remove these parentheses
50+
|
51+
LL - let _z: ([u8; (3)]);
52+
LL + let _z: ([u8; 3]);
53+
|
54+
55+
error: aborting due to 4 previous errors
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![warn(unused)]
2+
#![deny(warnings)]
3+
4+
struct Inv<'a>(&'a mut &'a ());
5+
6+
trait Trait<'a> {}
7+
impl<'b> Trait<'b> for for<'a> fn(Inv<'a>) {}
8+
9+
fn with_bound()
10+
where
11+
for<'b> (for<'a> fn(Inv<'a>)): Trait<'b>, //~ ERROR unnecessary parentheses around type
12+
{}
13+
14+
trait Hello<T> {}
15+
fn with_dyn_bound<T>()
16+
where
17+
(dyn Hello<(for<'b> fn(&'b ()))>): Hello<T> //~ ERROR unnecessary parentheses around type
18+
{}
19+
20+
fn main() {
21+
with_bound();
22+
with_dyn_bound();
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error: unnecessary parentheses around type
2+
--> $DIR/issue-105061-should-lint.rs:11:13
3+
|
4+
LL | for<'b> (for<'a> fn(Inv<'a>)): Trait<'b>,
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-105061-should-lint.rs:2:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unused_parens)]` implied by `#[deny(warnings)]`
13+
help: remove these parentheses
14+
|
15+
LL - for<'b> (for<'a> fn(Inv<'a>)): Trait<'b>,
16+
LL + for<'b> for<'a> fn(Inv<'a>): Trait<'b>,
17+
|
18+
19+
error: unnecessary parentheses around type
20+
--> $DIR/issue-105061-should-lint.rs:17:16
21+
|
22+
LL | (dyn Hello<(for<'b> fn(&'b ()))>): Hello<T>
23+
| ^ ^
24+
|
25+
help: remove these parentheses
26+
|
27+
LL - (dyn Hello<(for<'b> fn(&'b ()))>): Hello<T>
28+
LL + (dyn Hello<for<'b> fn(&'b ())>): Hello<T>
29+
|
30+
31+
error: aborting due to 2 previous errors
32+

tests/ui/lint/unused/issue-105061.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![warn(unused)]
2+
#![deny(warnings)]
3+
4+
struct Inv<'a>(&'a mut &'a ());
5+
6+
trait Trait {}
7+
impl Trait for (for<'a> fn(Inv<'a>),) {}
8+
9+
10+
fn with_bound()
11+
where
12+
((for<'a> fn(Inv<'a>)),): Trait, //~ ERROR unnecessary parentheses around type
13+
{}
14+
15+
fn main() {
16+
with_bound();
17+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: unnecessary parentheses around type
2+
--> $DIR/issue-105061.rs:12:6
3+
|
4+
LL | ((for<'a> fn(Inv<'a>)),): Trait,
5+
| ^ ^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/issue-105061.rs:2:9
9+
|
10+
LL | #![deny(warnings)]
11+
| ^^^^^^^^
12+
= note: `#[deny(unused_parens)]` implied by `#[deny(warnings)]`
13+
help: remove these parentheses
14+
|
15+
LL - ((for<'a> fn(Inv<'a>)),): Trait,
16+
LL + (for<'a> fn(Inv<'a>),): Trait,
17+
|
18+
19+
error: aborting due to previous error
20+

0 commit comments

Comments
 (0)