Skip to content

Commit 9175247

Browse files
authored
Rollup merge of rust-lang#65112 - jack-t:type-parens-lint, r=varkor
Add lint and tests for unnecessary parens around types This is my first contribution to the Rust project, so I apologize if I'm not doing things the right way. The PR fixes rust-lang#64169. It adds a lint and tests for unnecessary parentheses around types. I've run `tidy` and `rustfmt` — I'm not totally sure it worked right, though — and I've tried to follow the instructions linked in the readme. I tried to think through all the variants of `ast::TyKind` to find exceptions to this lint, and I could only find the one mentioned in the original issue, which concerns types with `dyn`. I'm not a Rust expert, thought, so I may well be missing something. There's also a problem with getting this to build. The new lint catches several things in the, e.g., `core`. Because `x.py` seems to build with an equivalent of `-Werror`, what would have been warnings cause the build to break. I got it to build and the tests to pass with `--warnings warn` on my `x.py build` and `x.py test` commands.
2 parents 01e5d91 + 08ca236 commit 9175247

File tree

14 files changed

+65
-27
lines changed

14 files changed

+65
-27
lines changed

src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
6262
&self,
6363
arg: &'tcx hir::Ty,
6464
br: &ty::BoundRegion,
65-
) -> Option<(&'tcx hir::Ty)> {
65+
) -> Option<&'tcx hir::Ty> {
6666
let mut nested_visitor = FindNestedTypeVisitor {
6767
tcx: self.tcx(),
6868
bound_region: *br,

src/librustc_data_structures/owning_ref/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1046,14 +1046,14 @@ unsafe impl<O, T: ?Sized> CloneStableAddress for OwningRef<O, T>
10461046
where O: CloneStableAddress {}
10471047

10481048
unsafe impl<O, T: ?Sized> Send for OwningRef<O, T>
1049-
where O: Send, for<'a> (&'a T): Send {}
1049+
where O: Send, for<'a> &'a T: Send {}
10501050
unsafe impl<O, T: ?Sized> Sync for OwningRef<O, T>
1051-
where O: Sync, for<'a> (&'a T): Sync {}
1051+
where O: Sync, for<'a> &'a T: Sync {}
10521052

10531053
unsafe impl<O, T: ?Sized> Send for OwningRefMut<O, T>
1054-
where O: Send, for<'a> (&'a mut T): Send {}
1054+
where O: Send, for<'a> &'a mut T: Send {}
10551055
unsafe impl<O, T: ?Sized> Sync for OwningRefMut<O, T>
1056-
where O: Sync, for<'a> (&'a mut T): Sync {}
1056+
where O: Sync, for<'a> &'a mut T: Sync {}
10571057

10581058
impl Debug for dyn Erased {
10591059
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

src/librustc_lint/unused.rs

+19
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,25 @@ impl EarlyLintPass for UnusedParens {
598598
fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) {
599599
self.check_unused_parens_pat(cx, &arm.pat, false, false);
600600
}
601+
602+
fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) {
603+
if let &ast::TyKind::Paren(ref r) = &ty.kind {
604+
match &r.kind {
605+
&ast::TyKind::TraitObject(..) => {}
606+
&ast::TyKind::ImplTrait(_, ref bounds) if bounds.len() > 1 => {}
607+
_ => {
608+
let pattern_text = if let Ok(snippet) = cx.sess().source_map()
609+
.span_to_snippet(ty.span) {
610+
snippet
611+
} else {
612+
pprust::ty_to_string(ty)
613+
};
614+
615+
Self::remove_outer_parens(cx, ty.span, &pattern_text, "type", (false, false));
616+
}
617+
}
618+
}
619+
}
601620
}
602621

603622
declare_lint! {

src/librustc_mir/borrow_check/prefixes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(super) struct Prefixes<'cx, 'tcx> {
2929
body: &'cx Body<'tcx>,
3030
tcx: TyCtxt<'tcx>,
3131
kind: PrefixSet,
32-
next: Option<(PlaceRef<'cx, 'tcx>)>,
32+
next: Option<PlaceRef<'cx, 'tcx>>,
3333
}
3434

3535
#[derive(Copy, Clone, PartialEq, Eq, Debug)]

src/libstd/collections/hash/map.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
18181818
type Item = &'a K;
18191819

18201820
#[inline]
1821-
fn next(&mut self) -> Option<(&'a K)> {
1821+
fn next(&mut self) -> Option<&'a K> {
18221822
self.inner.next().map(|(k, _)| k)
18231823
}
18241824
#[inline]
@@ -1841,7 +1841,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
18411841
type Item = &'a V;
18421842

18431843
#[inline]
1844-
fn next(&mut self) -> Option<(&'a V)> {
1844+
fn next(&mut self) -> Option<&'a V> {
18451845
self.inner.next().map(|(_, v)| v)
18461846
}
18471847
#[inline]
@@ -1864,7 +1864,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
18641864
type Item = &'a mut V;
18651865

18661866
#[inline]
1867-
fn next(&mut self) -> Option<(&'a mut V)> {
1867+
fn next(&mut self) -> Option<&'a mut V> {
18681868
self.inner.next().map(|(_, v)| v)
18691869
}
18701870
#[inline]

src/test/ui/alignment-gep-tup-like-1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
2828
box Invoker {
2929
a: a,
3030
b: b,
31-
} as (Box<dyn Invokable<A>+'static>)
31+
} as Box<dyn Invokable<A>+'static>
3232
}
3333

3434
pub fn main() {

src/test/ui/as-precedence.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-pass
22

3+
#[allow(unused_parens)]
34
fn main() {
45
assert_eq!(3 as usize * 3, 9);
56
assert_eq!(3 as (usize) * 3, 9);

src/test/ui/close-over-big-then-small-data.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn f<A:Clone + 'static>(a: A, b: u16) -> Box<dyn Invokable<A>+'static> {
3030
box Invoker {
3131
a: a,
3232
b: b,
33-
} as (Box<dyn Invokable<A>+'static>)
33+
} as Box<dyn Invokable<A>+'static>
3434
}
3535

3636
pub fn main() {

src/test/ui/functions-closures/closure-to-fn-coercion.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const BAR: [fn(&mut u32); 5] = [
1010
|v: &mut u32| *v += 3,
1111
|v: &mut u32| *v += 4,
1212
];
13-
fn func_specific() -> (fn() -> u32) {
13+
fn func_specific() -> fn() -> u32 {
1414
|| return 42
1515
}
1616

src/test/ui/lint/lint-unnecessary-parens.rs

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ fn bar(y: bool) -> X {
1313
return (X { y }); //~ ERROR unnecessary parentheses around `return` value
1414
}
1515

16+
fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parentheses around type
17+
panic!()
18+
}
19+
20+
trait Trait {
21+
fn test(&self);
22+
}
23+
24+
fn passes_unused_parens_lint() -> &'static (dyn Trait) {
25+
panic!()
26+
}
27+
1628
fn main() {
1729
foo();
1830
bar((true)); //~ ERROR unnecessary parentheses around function argument

src/test/ui/lint/lint-unnecessary-parens.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,79 @@ error: unnecessary parentheses around `return` value
1616
LL | return (X { y });
1717
| ^^^^^^^^^ help: remove these parentheses
1818

19+
error: unnecessary parentheses around type
20+
--> $DIR/lint-unnecessary-parens.rs:16:42
21+
|
22+
LL | fn unused_parens_around_return_type() -> (u32) {
23+
| ^^^^^ help: remove these parentheses
24+
1925
error: unnecessary parentheses around function argument
20-
--> $DIR/lint-unnecessary-parens.rs:18:9
26+
--> $DIR/lint-unnecessary-parens.rs:30:9
2127
|
2228
LL | bar((true));
2329
| ^^^^^^ help: remove these parentheses
2430

2531
error: unnecessary parentheses around `if` condition
26-
--> $DIR/lint-unnecessary-parens.rs:20:8
32+
--> $DIR/lint-unnecessary-parens.rs:32:8
2733
|
2834
LL | if (true) {}
2935
| ^^^^^^ help: remove these parentheses
3036

3137
error: unnecessary parentheses around `while` condition
32-
--> $DIR/lint-unnecessary-parens.rs:21:11
38+
--> $DIR/lint-unnecessary-parens.rs:33:11
3339
|
3440
LL | while (true) {}
3541
| ^^^^^^ help: remove these parentheses
3642

3743
warning: denote infinite loops with `loop { ... }`
38-
--> $DIR/lint-unnecessary-parens.rs:21:5
44+
--> $DIR/lint-unnecessary-parens.rs:33:5
3945
|
4046
LL | while (true) {}
4147
| ^^^^^^^^^^^^ help: use `loop`
4248
|
4349
= note: `#[warn(while_true)]` on by default
4450

4551
error: unnecessary parentheses around `match` head expression
46-
--> $DIR/lint-unnecessary-parens.rs:23:11
52+
--> $DIR/lint-unnecessary-parens.rs:35:11
4753
|
4854
LL | match (true) {
4955
| ^^^^^^ help: remove these parentheses
5056

5157
error: unnecessary parentheses around `let` head expression
52-
--> $DIR/lint-unnecessary-parens.rs:26:16
58+
--> $DIR/lint-unnecessary-parens.rs:38:16
5359
|
5460
LL | if let 1 = (1) {}
5561
| ^^^ help: remove these parentheses
5662

5763
error: unnecessary parentheses around `let` head expression
58-
--> $DIR/lint-unnecessary-parens.rs:27:19
64+
--> $DIR/lint-unnecessary-parens.rs:39:19
5965
|
6066
LL | while let 1 = (2) {}
6167
| ^^^ help: remove these parentheses
6268

6369
error: unnecessary parentheses around method argument
64-
--> $DIR/lint-unnecessary-parens.rs:41:24
70+
--> $DIR/lint-unnecessary-parens.rs:53:24
6571
|
6672
LL | X { y: false }.foo((true));
6773
| ^^^^^^ help: remove these parentheses
6874

6975
error: unnecessary parentheses around assigned value
70-
--> $DIR/lint-unnecessary-parens.rs:43:18
76+
--> $DIR/lint-unnecessary-parens.rs:55:18
7177
|
7278
LL | let mut _a = (0);
7379
| ^^^ help: remove these parentheses
7480

7581
error: unnecessary parentheses around assigned value
76-
--> $DIR/lint-unnecessary-parens.rs:44:10
82+
--> $DIR/lint-unnecessary-parens.rs:56:10
7783
|
7884
LL | _a = (0);
7985
| ^^^ help: remove these parentheses
8086

8187
error: unnecessary parentheses around assigned value
82-
--> $DIR/lint-unnecessary-parens.rs:45:11
88+
--> $DIR/lint-unnecessary-parens.rs:57:11
8389
|
8490
LL | _a += (1);
8591
| ^^^ help: remove these parentheses
8692

87-
error: aborting due to 12 previous errors
93+
error: aborting due to 13 previous errors
8894

src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn october<'b, T>(s: &'b T) -> &'b T {
1515
s
1616
}
1717

18-
fn november<'a>(s: &'a str) -> (&'a str) {
18+
fn november<'a>(s: &'a str) -> &'a str {
1919
//~^ ERROR lifetime parameter `'b` never used
2020
//~| HELP elide the unused lifetime
2121
s

src/test/ui/single-use-lifetime/zero-uses-in-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn october<'a, 'b, T>(s: &'b T) -> &'b T {
1515
s
1616
}
1717

18-
fn november<'a, 'b>(s: &'a str) -> (&'a str) {
18+
fn november<'a, 'b>(s: &'a str) -> &'a str {
1919
//~^ ERROR lifetime parameter `'b` never used
2020
//~| HELP elide the unused lifetime
2121
s

src/test/ui/single-use-lifetime/zero-uses-in-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL | fn october<'a, 'b, T>(s: &'b T) -> &'b T {
2121
error: lifetime parameter `'b` never used
2222
--> $DIR/zero-uses-in-fn.rs:18:17
2323
|
24-
LL | fn november<'a, 'b>(s: &'a str) -> (&'a str) {
24+
LL | fn november<'a, 'b>(s: &'a str) -> &'a str {
2525
| --^^
2626
| |
2727
| help: elide the unused lifetime

0 commit comments

Comments
 (0)