rust/tests/ui/lint/lint-unnecessary-parens.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

98 lines
3.2 KiB
Rust
Raw Normal View History

2020-07-02 00:32:12 -05:00
// run-rustfix
2014-10-27 17:37:07 -05:00
#![deny(unused_parens)]
2020-07-02 00:32:12 -05:00
#![allow(while_true)] // for rustfix
#[derive(Eq, PartialEq)]
struct X { y: bool }
impl X {
fn foo(&self, conjunct: bool) -> bool { self.y && conjunct }
}
fn foo() -> isize {
2015-01-31 10:23:42 -06:00
return (1); //~ ERROR unnecessary parentheses around `return` value
}
fn bar(y: bool) -> X {
return (X { y }); //~ ERROR unnecessary parentheses around `return` value
}
2020-07-02 00:32:12 -05:00
pub fn unused_parens_around_return_type() -> (u32) { //~ ERROR unnecessary parentheses around type
panic!()
}
2020-07-02 00:32:12 -05:00
pub fn unused_parens_around_block_return() -> u32 {
let _foo = {
(5) //~ ERROR unnecessary parentheses around block return value
};
(5) //~ ERROR unnecessary parentheses around block return value
}
2020-07-02 00:32:12 -05:00
pub trait Trait {
fn test(&self);
}
2020-07-02 00:32:12 -05:00
pub fn passes_unused_parens_lint() -> &'static (dyn Trait) {
panic!()
}
pub fn parens_with_keyword(e: &[()]) -> i32 {
if(true) {} //~ ERROR unnecessary parentheses around `if`
while(true) {} //~ ERROR unnecessary parentheses around `while`
for _ in(e) {} //~ ERROR unnecessary parentheses around `for`
match(1) { _ => ()} //~ ERROR unnecessary parentheses around `match`
return(1); //~ ERROR unnecessary parentheses around `return` value
}
2019-12-03 05:35:09 -06:00
macro_rules! baz {
($($foo:expr),+) => {
($($foo),*)
}
}
2020-07-02 00:32:12 -05:00
pub const CONST_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
pub static STATIC_ITEM: usize = (10); //~ ERROR unnecessary parentheses around assigned value
fn main() {
foo();
bar((true)); //~ ERROR unnecessary parentheses around function argument
if (true) {} //~ ERROR unnecessary parentheses around `if` condition
while (true) {} //~ ERROR unnecessary parentheses around `while` condition
2020-03-31 11:42:54 -05:00
match (true) { //~ ERROR unnecessary parentheses around `match` scrutinee expression
_ => {}
}
2020-03-31 11:42:54 -05:00
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 }) {}
if (X { y: true } == v) {}
if (X { y: false }.y) {}
// this shouldn't warn, because the parens are necessary to disambiguate let chains
if let true = (true && false) {}
while (X { y: false }.foo(true)) {}
while (true | X { y: false }.y) {}
match (X { y: false }) {
_ => {}
}
X { y: false }.foo((true)); //~ ERROR unnecessary parentheses around method argument
2015-01-31 10:23:42 -06:00
let mut _a = (0); //~ ERROR unnecessary parentheses around assigned value
_a = (0); //~ ERROR unnecessary parentheses around assigned value
_a += (1); //~ ERROR unnecessary parentheses around assigned value
2019-12-03 05:35:09 -06:00
let(mut _a) = 3; //~ ERROR unnecessary parentheses around pattern
let (mut _a) = 3; //~ ERROR unnecessary parentheses around pattern
let( mut _a) = 3; //~ ERROR unnecessary parentheses around pattern
let(_a) = 3; //~ ERROR unnecessary parentheses around pattern
let (_a) = 3; //~ ERROR unnecessary parentheses around pattern
let( _a) = 3; //~ ERROR unnecessary parentheses around pattern
2019-12-03 05:35:09 -06:00
let _a = baz!(3, 4);
let _b = baz!(3);
}