From 08ca2360c4e817acab717dfb7e5a93d5af35cc06 Mon Sep 17 00:00:00 2001 From: jack-t Date: Fri, 4 Oct 2019 01:56:57 -0400 Subject: [PATCH] Add lint for unnecessary parens around types --- .../nice_region_error/find_anon_type.rs | 2 +- .../owning_ref/mod.rs | 8 ++--- src/librustc_lint/unused.rs | 19 ++++++++++++ src/librustc_mir/borrow_check/prefixes.rs | 2 +- src/libstd/collections/hash/map.rs | 6 ++-- src/test/ui/alignment-gep-tup-like-1.rs | 2 +- src/test/ui/as-precedence.rs | 1 + src/test/ui/close-over-big-then-small-data.rs | 2 +- .../closure-to-fn-coercion.rs | 2 +- src/test/ui/lint/lint-unnecessary-parens.rs | 12 ++++++++ .../ui/lint/lint-unnecessary-parens.stderr | 30 +++++++++++-------- .../single-use-lifetime/zero-uses-in-fn.fixed | 2 +- .../ui/single-use-lifetime/zero-uses-in-fn.rs | 2 +- .../zero-uses-in-fn.stderr | 2 +- 14 files changed, 65 insertions(+), 27 deletions(-) diff --git a/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs b/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs index 9c362a5e20791..f4751e591bf3c 100644 --- a/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs +++ b/src/librustc/infer/error_reporting/nice_region_error/find_anon_type.rs @@ -62,7 +62,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> { &self, arg: &'tcx hir::Ty, br: &ty::BoundRegion, - ) -> Option<(&'tcx hir::Ty)> { + ) -> Option<&'tcx hir::Ty> { let mut nested_visitor = FindNestedTypeVisitor { tcx: self.tcx(), bound_region: *br, diff --git a/src/librustc_data_structures/owning_ref/mod.rs b/src/librustc_data_structures/owning_ref/mod.rs index b835b1706b85f..0213eb4f2a2a4 100644 --- a/src/librustc_data_structures/owning_ref/mod.rs +++ b/src/librustc_data_structures/owning_ref/mod.rs @@ -1046,14 +1046,14 @@ unsafe impl CloneStableAddress for OwningRef where O: CloneStableAddress {} unsafe impl Send for OwningRef - where O: Send, for<'a> (&'a T): Send {} + where O: Send, for<'a> &'a T: Send {} unsafe impl Sync for OwningRef - where O: Sync, for<'a> (&'a T): Sync {} + where O: Sync, for<'a> &'a T: Sync {} unsafe impl Send for OwningRefMut - where O: Send, for<'a> (&'a mut T): Send {} + where O: Send, for<'a> &'a mut T: Send {} unsafe impl Sync for OwningRefMut - where O: Sync, for<'a> (&'a mut T): Sync {} + where O: Sync, for<'a> &'a mut T: Sync {} impl Debug for dyn Erased { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 3b3995832cb4c..7a168baacaf81 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -603,6 +603,25 @@ impl EarlyLintPass for UnusedParens { fn check_arm(&mut self, cx: &EarlyContext<'_>, arm: &ast::Arm) { self.check_unused_parens_pat(cx, &arm.pat, false, false); } + + fn check_ty(&mut self, cx: &EarlyContext<'_>, ty: &ast::Ty) { + if let &ast::TyKind::Paren(ref r) = &ty.kind { + match &r.kind { + &ast::TyKind::TraitObject(..) => {} + &ast::TyKind::ImplTrait(_, ref bounds) if bounds.len() > 1 => {} + _ => { + let pattern_text = if let Ok(snippet) = cx.sess().source_map() + .span_to_snippet(ty.span) { + snippet + } else { + pprust::ty_to_string(ty) + }; + + Self::remove_outer_parens(cx, ty.span, &pattern_text, "type", (false, false)); + } + } + } + } } declare_lint! { diff --git a/src/librustc_mir/borrow_check/prefixes.rs b/src/librustc_mir/borrow_check/prefixes.rs index a46a1cc5647a9..1be1fcef61b6e 100644 --- a/src/librustc_mir/borrow_check/prefixes.rs +++ b/src/librustc_mir/borrow_check/prefixes.rs @@ -29,7 +29,7 @@ pub(super) struct Prefixes<'cx, 'tcx> { body: &'cx Body<'tcx>, tcx: TyCtxt<'tcx>, kind: PrefixSet, - next: Option<(PlaceRef<'cx, 'tcx>)>, + next: Option>, } #[derive(Copy, Clone, PartialEq, Eq, Debug)] diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index ff50051ef5040..941c81eaff264 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -1818,7 +1818,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> { type Item = &'a K; #[inline] - fn next(&mut self) -> Option<(&'a K)> { + fn next(&mut self) -> Option<&'a K> { self.inner.next().map(|(k, _)| k) } #[inline] @@ -1841,7 +1841,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> { type Item = &'a V; #[inline] - fn next(&mut self) -> Option<(&'a V)> { + fn next(&mut self) -> Option<&'a V> { self.inner.next().map(|(_, v)| v) } #[inline] @@ -1864,7 +1864,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> { type Item = &'a mut V; #[inline] - fn next(&mut self) -> Option<(&'a mut V)> { + fn next(&mut self) -> Option<&'a mut V> { self.inner.next().map(|(_, v)| v) } #[inline] diff --git a/src/test/ui/alignment-gep-tup-like-1.rs b/src/test/ui/alignment-gep-tup-like-1.rs index c51c56b089926..adbd05ed8c175 100644 --- a/src/test/ui/alignment-gep-tup-like-1.rs +++ b/src/test/ui/alignment-gep-tup-like-1.rs @@ -28,7 +28,7 @@ fn f(a: A, b: u16) -> Box+'static> { box Invoker { a: a, b: b, - } as (Box+'static>) + } as Box+'static> } pub fn main() { diff --git a/src/test/ui/as-precedence.rs b/src/test/ui/as-precedence.rs index a9f6fceb08ff0..feb0cb30ccacc 100644 --- a/src/test/ui/as-precedence.rs +++ b/src/test/ui/as-precedence.rs @@ -1,5 +1,6 @@ // run-pass +#[allow(unused_parens)] fn main() { assert_eq!(3 as usize * 3, 9); assert_eq!(3 as (usize) * 3, 9); diff --git a/src/test/ui/close-over-big-then-small-data.rs b/src/test/ui/close-over-big-then-small-data.rs index 40e5f500df4a8..4d6edf4ecb0f3 100644 --- a/src/test/ui/close-over-big-then-small-data.rs +++ b/src/test/ui/close-over-big-then-small-data.rs @@ -30,7 +30,7 @@ fn f(a: A, b: u16) -> Box+'static> { box Invoker { a: a, b: b, - } as (Box+'static>) + } as Box+'static> } pub fn main() { diff --git a/src/test/ui/functions-closures/closure-to-fn-coercion.rs b/src/test/ui/functions-closures/closure-to-fn-coercion.rs index 4f43c2bb53847..87ba488b5aef2 100644 --- a/src/test/ui/functions-closures/closure-to-fn-coercion.rs +++ b/src/test/ui/functions-closures/closure-to-fn-coercion.rs @@ -10,7 +10,7 @@ const BAR: [fn(&mut u32); 5] = [ |v: &mut u32| *v += 3, |v: &mut u32| *v += 4, ]; -fn func_specific() -> (fn() -> u32) { +fn func_specific() -> fn() -> u32 { || return 42 } diff --git a/src/test/ui/lint/lint-unnecessary-parens.rs b/src/test/ui/lint/lint-unnecessary-parens.rs index 811bc87eb0e2e..9f42b855a870d 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.rs +++ b/src/test/ui/lint/lint-unnecessary-parens.rs @@ -13,6 +13,18 @@ fn bar(y: bool) -> X { return (X { y }); //~ ERROR unnecessary parentheses around `return` value } +fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parentheses around type + panic!() +} + +trait Trait { + fn test(&self); +} + +fn passes_unused_parens_lint() -> &'static (dyn Trait) { + panic!() +} + fn main() { foo(); bar((true)); //~ ERROR unnecessary parentheses around function argument diff --git a/src/test/ui/lint/lint-unnecessary-parens.stderr b/src/test/ui/lint/lint-unnecessary-parens.stderr index 83b247a4a6053..adc1069b64d62 100644 --- a/src/test/ui/lint/lint-unnecessary-parens.stderr +++ b/src/test/ui/lint/lint-unnecessary-parens.stderr @@ -16,26 +16,32 @@ error: unnecessary parentheses around `return` value LL | return (X { y }); | ^^^^^^^^^ help: remove these parentheses +error: unnecessary parentheses around type + --> $DIR/lint-unnecessary-parens.rs:16:42 + | +LL | fn unused_parens_around_return_type() -> (u32) { + | ^^^^^ help: remove these parentheses + error: unnecessary parentheses around function argument - --> $DIR/lint-unnecessary-parens.rs:18:9 + --> $DIR/lint-unnecessary-parens.rs:30:9 | LL | bar((true)); | ^^^^^^ help: remove these parentheses error: unnecessary parentheses around `if` condition - --> $DIR/lint-unnecessary-parens.rs:20:8 + --> $DIR/lint-unnecessary-parens.rs:32:8 | LL | if (true) {} | ^^^^^^ help: remove these parentheses error: unnecessary parentheses around `while` condition - --> $DIR/lint-unnecessary-parens.rs:21:11 + --> $DIR/lint-unnecessary-parens.rs:33:11 | LL | while (true) {} | ^^^^^^ help: remove these parentheses warning: denote infinite loops with `loop { ... }` - --> $DIR/lint-unnecessary-parens.rs:21:5 + --> $DIR/lint-unnecessary-parens.rs:33:5 | LL | while (true) {} | ^^^^^^^^^^^^ help: use `loop` @@ -43,46 +49,46 @@ LL | while (true) {} = note: `#[warn(while_true)]` on by default error: unnecessary parentheses around `match` head expression - --> $DIR/lint-unnecessary-parens.rs:23:11 + --> $DIR/lint-unnecessary-parens.rs:35:11 | LL | match (true) { | ^^^^^^ help: remove these parentheses error: unnecessary parentheses around `let` head expression - --> $DIR/lint-unnecessary-parens.rs:26:16 + --> $DIR/lint-unnecessary-parens.rs:38:16 | LL | if let 1 = (1) {} | ^^^ help: remove these parentheses error: unnecessary parentheses around `let` head expression - --> $DIR/lint-unnecessary-parens.rs:27:19 + --> $DIR/lint-unnecessary-parens.rs:39:19 | LL | while let 1 = (2) {} | ^^^ help: remove these parentheses error: unnecessary parentheses around method argument - --> $DIR/lint-unnecessary-parens.rs:41:24 + --> $DIR/lint-unnecessary-parens.rs:53:24 | LL | X { y: false }.foo((true)); | ^^^^^^ help: remove these parentheses error: unnecessary parentheses around assigned value - --> $DIR/lint-unnecessary-parens.rs:43:18 + --> $DIR/lint-unnecessary-parens.rs:55:18 | LL | let mut _a = (0); | ^^^ help: remove these parentheses error: unnecessary parentheses around assigned value - --> $DIR/lint-unnecessary-parens.rs:44:10 + --> $DIR/lint-unnecessary-parens.rs:56:10 | LL | _a = (0); | ^^^ help: remove these parentheses error: unnecessary parentheses around assigned value - --> $DIR/lint-unnecessary-parens.rs:45:11 + --> $DIR/lint-unnecessary-parens.rs:57:11 | LL | _a += (1); | ^^^ help: remove these parentheses -error: aborting due to 12 previous errors +error: aborting due to 13 previous errors diff --git a/src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed b/src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed index 89607af260a52..0f26a975a370f 100644 --- a/src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed +++ b/src/test/ui/single-use-lifetime/zero-uses-in-fn.fixed @@ -15,7 +15,7 @@ fn october<'b, T>(s: &'b T) -> &'b T { s } -fn november<'a>(s: &'a str) -> (&'a str) { +fn november<'a>(s: &'a str) -> &'a str { //~^ ERROR lifetime parameter `'b` never used //~| HELP elide the unused lifetime s diff --git a/src/test/ui/single-use-lifetime/zero-uses-in-fn.rs b/src/test/ui/single-use-lifetime/zero-uses-in-fn.rs index be0bdb9b62851..7f9504fe5a90a 100644 --- a/src/test/ui/single-use-lifetime/zero-uses-in-fn.rs +++ b/src/test/ui/single-use-lifetime/zero-uses-in-fn.rs @@ -15,7 +15,7 @@ fn october<'a, 'b, T>(s: &'b T) -> &'b T { s } -fn november<'a, 'b>(s: &'a str) -> (&'a str) { +fn november<'a, 'b>(s: &'a str) -> &'a str { //~^ ERROR lifetime parameter `'b` never used //~| HELP elide the unused lifetime s diff --git a/src/test/ui/single-use-lifetime/zero-uses-in-fn.stderr b/src/test/ui/single-use-lifetime/zero-uses-in-fn.stderr index 2ccba796d4229..b9c3bd89748ff 100644 --- a/src/test/ui/single-use-lifetime/zero-uses-in-fn.stderr +++ b/src/test/ui/single-use-lifetime/zero-uses-in-fn.stderr @@ -21,7 +21,7 @@ LL | fn october<'a, 'b, T>(s: &'b T) -> &'b T { error: lifetime parameter `'b` never used --> $DIR/zero-uses-in-fn.rs:18:17 | -LL | fn november<'a, 'b>(s: &'a str) -> (&'a str) { +LL | fn november<'a, 'b>(s: &'a str) -> &'a str { | --^^ | | | help: elide the unused lifetime