Rollup merge of #70081 - lcnr:issue68387, r=varkor
add `unused_braces` lint Add the lint `unused_braces` which is warn by default. `unused_parens` is also extended and now checks anon consts. closes #68387 r? @varkor
This commit is contained in:
commit
8993358e77
44 changed files with 600 additions and 217 deletions
|
|
@ -4,7 +4,7 @@
|
|||
|
||||
struct FakeArray<T, const N: usize>(T);
|
||||
|
||||
impl<T, const N: usize> FakeArray<T, { N }> {
|
||||
impl<T, const N: usize> FakeArray<T, N> {
|
||||
fn len(&self) -> usize {
|
||||
N
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ fn test_big_vec() {}
|
|||
#[cfg(target_pointer_width = "64")]
|
||||
fn test_big_vec()
|
||||
{
|
||||
assert_eq!(size_of::<[u8; (1 << 32)]>(), (1 << 32));
|
||||
assert_eq!(size_of::<[u8; 1 << 32]>(), (1 << 32));
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
|
||||
fn force<F>(f: F) -> isize where F: FnOnce() -> isize { return f(); }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![allow(non_snake_case)]
|
||||
#![allow(unused_variables)]
|
||||
// Test that destructors for rvalue temporaries run either at end of
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
use std::cell::RefCell;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
#![allow(dead_code)]
|
||||
// pretty-expanded FIXME #23616
|
||||
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ trait HasSize {
|
|||
const SIZE: usize;
|
||||
}
|
||||
|
||||
impl<const X: usize> HasSize for ArrayHolder<{ X }> {
|
||||
impl<const X: usize> HasSize for ArrayHolder<X> {
|
||||
const SIZE: usize = X;
|
||||
}
|
||||
|
||||
struct ArrayHolder<const X: usize>([u32; X]);
|
||||
|
||||
impl<const X: usize> ArrayHolder<{ X }> {
|
||||
impl<const X: usize> ArrayHolder<X> {
|
||||
pub const fn new() -> Self {
|
||||
ArrayHolder([0; Self::SIZE])
|
||||
//~^ ERROR: mismatched types
|
||||
|
|
|
|||
|
|
@ -13,4 +13,4 @@ trait Foo<const X: usize> {
|
|||
}
|
||||
}
|
||||
|
||||
impl Foo<{3}> for () {}
|
||||
impl Foo<3> for () {}
|
||||
|
|
|
|||
13
src/test/ui/const-generics/unused_braces.rs
Normal file
13
src/test/ui/const-generics/unused_braces.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
// check-pass
|
||||
#![warn(unused_braces)]
|
||||
|
||||
#![feature(const_generics)]
|
||||
//~^ WARN the feature `const_generics` is incomplete and may cause the compiler to crash
|
||||
|
||||
struct A<const N: usize>;
|
||||
|
||||
fn main() {
|
||||
let _: A<7>; // ok
|
||||
let _: A<{ 7 }>; //~ WARN unnecessary braces
|
||||
let _: A<{ 3 + 5 }>; // ok
|
||||
}
|
||||
20
src/test/ui/const-generics/unused_braces.stderr
Normal file
20
src/test/ui/const-generics/unused_braces.stderr
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
warning: the feature `const_generics` is incomplete and may cause the compiler to crash
|
||||
--> $DIR/unused_braces.rs:4:12
|
||||
|
|
||||
LL | #![feature(const_generics)]
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
|
||||
= note: `#[warn(incomplete_features)]` on by default
|
||||
|
||||
warning: unnecessary braces around const expression
|
||||
--> $DIR/unused_braces.rs:11:14
|
||||
|
|
||||
LL | let _: A<{ 7 }>;
|
||||
| ^^^^^ help: remove these braces
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/unused_braces.rs:2:9
|
||||
|
|
||||
LL | #![warn(unused_braces)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_unsafe)]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
fn test_generic<T, F>(expected: Box<T>, eq: F) where T: Clone, F: FnOnce(Box<T>, Box<T>) -> bool {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
fn test_generic<T, F>(expected: T, eq: F) where T: Clone, F: FnOnce(T, T) -> bool {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
|
||||
fn test_generic<T: Clone, F>(expected: T, eq: F) where F: FnOnce(T, T) -> bool {
|
||||
let actual: T = { expected.clone() };
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![feature(box_syntax)]
|
||||
|
||||
pub fn main() { let x: Box<_> = { box 100 }; assert_eq!(*x, 100); }
|
||||
|
|
|
|||
|
|
@ -1,10 +1,7 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
|
||||
|
||||
|
||||
// Tests for standalone blocks as expressions
|
||||
|
||||
fn test_basic() { let rs: bool = { true }; assert!((rs)); }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
|
||||
fn test_int() {
|
||||
fn f() -> isize { 10 }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
|
||||
fn foo(i: isize) -> isize { i + 1 }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
// run-pass
|
||||
// Test a rather underspecified example:
|
||||
|
||||
#![allow(unused_braces)]
|
||||
|
||||
pub fn main() {
|
||||
let f = {|i| i};
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
#![allow(unused_unsafe)]
|
||||
#![allow(unreachable_code)]
|
||||
// ignore-emscripten no threads support
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_parens)]
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
// Note: This test was used to demonstrate #5873 (now #23898).
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
fn main() {
|
||||
let v1 = { 1 + {2} * {3} };
|
||||
let v2 = 1 + {2} * {3} ;
|
||||
|
|
|
|||
|
|
@ -48,11 +48,11 @@ fn main() {
|
|||
if (true) {} //~ ERROR unnecessary parentheses around `if` condition
|
||||
while (true) {} //~ ERROR unnecessary parentheses around `while` condition
|
||||
//~^ WARN denote infinite loops with
|
||||
match (true) { //~ ERROR unnecessary parentheses around `match` head expression
|
||||
match (true) { //~ ERROR unnecessary parentheses around `match` scrutinee expression
|
||||
_ => {}
|
||||
}
|
||||
if let 1 = (1) {} //~ ERROR unnecessary parentheses around `let` head expression
|
||||
while let 1 = (2) {} //~ ERROR unnecessary parentheses around `let` head expression
|
||||
if let 1 = (1) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
|
||||
while let 1 = (2) {} //~ ERROR unnecessary parentheses around `let` scrutinee expression
|
||||
let v = X { y: false };
|
||||
// struct lits needs parens, so these shouldn't warn.
|
||||
if (v == X { y: true }) {}
|
||||
|
|
|
|||
|
|
@ -72,19 +72,19 @@ LL | while (true) {}
|
|||
|
|
||||
= note: `#[warn(while_true)]` on by default
|
||||
|
||||
error: unnecessary parentheses around `match` head expression
|
||||
error: unnecessary parentheses around `match` scrutinee expression
|
||||
--> $DIR/lint-unnecessary-parens.rs:51:11
|
||||
|
|
||||
LL | match (true) {
|
||||
| ^^^^^^ help: remove these parentheses
|
||||
|
||||
error: unnecessary parentheses around `let` head expression
|
||||
error: unnecessary parentheses around `let` scrutinee expression
|
||||
--> $DIR/lint-unnecessary-parens.rs:54:16
|
||||
|
|
||||
LL | if let 1 = (1) {}
|
||||
| ^^^ help: remove these parentheses
|
||||
|
||||
error: unnecessary parentheses around `let` head expression
|
||||
error: unnecessary parentheses around `let` scrutinee expression
|
||||
--> $DIR/lint-unnecessary-parens.rs:55:19
|
||||
|
|
||||
LL | while let 1 = (2) {}
|
||||
|
|
|
|||
31
src/test/ui/lint/unused_braces.rs
Normal file
31
src/test/ui/lint/unused_braces.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
// check-pass
|
||||
#![warn(unused_braces, unused_parens)]
|
||||
|
||||
fn main() {
|
||||
let _ = (7);
|
||||
//~^WARN unnecessary parentheses
|
||||
|
||||
let _ = { 7 };
|
||||
//~^ WARN unnecessary braces
|
||||
|
||||
if let 7 = { 7 } {
|
||||
//~^ WARN unnecessary braces
|
||||
}
|
||||
|
||||
let _: [u8; { 3 }];
|
||||
//~^ WARN unnecessary braces
|
||||
|
||||
// do not emit error for multiline blocks.
|
||||
let _ = {
|
||||
7
|
||||
};
|
||||
|
||||
// do not emit error for unsafe blocks.
|
||||
let _ = unsafe { 7 };
|
||||
|
||||
// do not emit error, as the `{` would then
|
||||
// be parsed as part of the `return`.
|
||||
if { return } {
|
||||
|
||||
}
|
||||
}
|
||||
36
src/test/ui/lint/unused_braces.stderr
Normal file
36
src/test/ui/lint/unused_braces.stderr
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
warning: unnecessary parentheses around assigned value
|
||||
--> $DIR/unused_braces.rs:5:13
|
||||
|
|
||||
LL | let _ = (7);
|
||||
| ^^^ help: remove these parentheses
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/unused_braces.rs:2:24
|
||||
|
|
||||
LL | #![warn(unused_braces, unused_parens)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: unnecessary braces around assigned value
|
||||
--> $DIR/unused_braces.rs:8:13
|
||||
|
|
||||
LL | let _ = { 7 };
|
||||
| ^^^^^ help: remove these braces
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/unused_braces.rs:2:9
|
||||
|
|
||||
LL | #![warn(unused_braces, unused_parens)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
warning: unnecessary braces around `let` scrutinee expression
|
||||
--> $DIR/unused_braces.rs:11:16
|
||||
|
|
||||
LL | if let 7 = { 7 } {
|
||||
| ^^^^^ help: remove these braces
|
||||
|
||||
warning: unnecessary braces around const expression
|
||||
--> $DIR/unused_braces.rs:15:17
|
||||
|
|
||||
LL | let _: [u8; { 3 }];
|
||||
| ^^^^^ help: remove these braces
|
||||
|
||||
22
src/test/ui/lint/unused_parens_borrow.rs
Normal file
22
src/test/ui/lint/unused_parens_borrow.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// check-pass
|
||||
#![warn(unused_braces)]
|
||||
|
||||
// changing `&{ expr }` to `&expr` changes the semantic of the program
|
||||
// so we should not warn this case
|
||||
|
||||
#[repr(packed)]
|
||||
struct A {
|
||||
a: u8,
|
||||
b: u32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = A {
|
||||
a: 42,
|
||||
b: 1729,
|
||||
};
|
||||
|
||||
let _ = &{ a.b };
|
||||
let _ = { a.b };
|
||||
//~^ WARN unnecessary braces
|
||||
}
|
||||
12
src/test/ui/lint/unused_parens_borrow.stderr
Normal file
12
src/test/ui/lint/unused_parens_borrow.stderr
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
warning: unnecessary braces around assigned value
|
||||
--> $DIR/unused_parens_borrow.rs:20:13
|
||||
|
|
||||
LL | let _ = { a.b };
|
||||
| ^^^^^^^ help: remove these braces
|
||||
|
|
||||
note: the lint level is defined here
|
||||
--> $DIR/unused_parens_borrow.rs:2:9
|
||||
|
|
||||
LL | #![warn(unused_braces)]
|
||||
| ^^^^^^^^^^^^^
|
||||
|
||||
|
|
@ -46,14 +46,14 @@ LL | while(true && false) {
|
|||
| ^^^^^^^^^^^^^^^ help: remove these parentheses
|
||||
|
||||
"}
|
||||
{"message":"unnecessary parentheses around `for` head expression","code":{"code":"unused_parens","explanation":null},"level":"error","spans":[{"file_name":"$DIR/unused_parens_remove_json_suggestion.rs","byte_start":987,"byte_end":995,"line_start":44,"line_end":44,"column_start":18,"column_end":26,"is_primary":true,"text":[{"text":" for _ in (0 .. 3){
|
||||
{"message":"unnecessary parentheses around `for` iterator expression","code":{"code":"unused_parens","explanation":null},"level":"error","spans":[{"file_name":"$DIR/unused_parens_remove_json_suggestion.rs","byte_start":987,"byte_end":995,"line_start":44,"line_end":44,"column_start":18,"column_end":26,"is_primary":true,"text":[{"text":" for _ in (0 .. 3){
|
||||
--> $DIR/unused_parens_remove_json_suggestion.rs:44:18
|
||||
|
|
||||
LL | for _ in (0 .. 3){
|
||||
| ^^^^^^^^ help: remove these parentheses
|
||||
|
||||
"}
|
||||
{"message":"unnecessary parentheses around `for` head expression","code":{"code":"unused_parens","explanation":null},"level":"error","spans":[{"file_name":"$DIR/unused_parens_remove_json_suggestion.rs","byte_start":1088,"byte_end":1096,"line_start":49,"line_end":49,"column_start":14,"column_end":22,"is_primary":true,"text":[{"text":" for _ in (0 .. 3) {
|
||||
{"message":"unnecessary parentheses around `for` iterator expression","code":{"code":"unused_parens","explanation":null},"level":"error","spans":[{"file_name":"$DIR/unused_parens_remove_json_suggestion.rs","byte_start":1088,"byte_end":1096,"line_start":49,"line_end":49,"column_start":14,"column_end":22,"is_primary":true,"text":[{"text":" for _ in (0 .. 3) {
|
||||
--> $DIR/unused_parens_remove_json_suggestion.rs:49:14
|
||||
|
|
||||
LL | for _ in (0 .. 3) {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces)]
|
||||
#![allow(unused_comparisons)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(unused_mut)]
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
// run-pass
|
||||
// Test inclusive range syntax.
|
||||
|
||||
#![feature(range_is_empty)]
|
||||
#![allow(unused_braces)]
|
||||
#![allow(unused_comparisons)]
|
||||
|
||||
use std::ops::RangeToInclusive;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
#![allow(non_camel_case_types)]
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
|
|
|
|||
|
|
@ -18,16 +18,16 @@ trait MyTrait<'a, const C: usize> {
|
|||
const MY_CONST: usize;
|
||||
}
|
||||
|
||||
impl<'a, const C: usize> MyTrait<'a, { C }> for MyStruct<{ C }> {
|
||||
impl<'a, const C: usize> MyTrait<'a, C> for MyStruct<C> {
|
||||
type MyItem = u8;
|
||||
const MY_CONST: usize = C;
|
||||
}
|
||||
|
||||
impl<'a, I, const C: usize> UnwrapItemsExt<'a, { C }> for I {
|
||||
type Iter = impl MyTrait<'a, { C }>;
|
||||
impl<'a, I, const C: usize> UnwrapItemsExt<'a, C> for I {
|
||||
type Iter = impl MyTrait<'a, C>;
|
||||
|
||||
fn unwrap_items(self) -> Self::Iter {
|
||||
MyStruct::<{ C }> {}
|
||||
MyStruct::<C> {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
// run-pass
|
||||
|
||||
#![allow(unused_braces, unused_parens)]
|
||||
#![feature(unsized_tuple_coercion, unsized_locals)]
|
||||
|
||||
struct A<X: ?Sized>(X);
|
||||
|
|
@ -30,7 +30,6 @@ fn main() {
|
|||
*foo()
|
||||
});
|
||||
udrop::<[u8]>({*foo()});
|
||||
#[allow(unused_parens)]
|
||||
udrop::<[u8]>((*foo()));
|
||||
udrop::<[u8]>((*tfoo()).1);
|
||||
*afoo() + 42;
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#![allow(non_camel_case_types)]
|
||||
#![allow(dead_code)]
|
||||
#![allow(unreachable_code)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_braces, unused_parens)]
|
||||
|
||||
#![recursion_limit = "256"]
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
// run-pass
|
||||
#![allow(unused_braces)]
|
||||
#![allow(unused_assignments)]
|
||||
|
||||
// Make sure that the constructor args are codegened for zero-sized tuple structs
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue