Auto merge of #7894 - Serial-ATA:extend-author-lint, r=camsteffen

Extend author lint

changelog: none

* Print float and int suffixes
* Print labels
* Struct field checks
* Repeat length expression check
* Destructure method calls
* Destructure closures
This commit is contained in:
bors 2021-11-06 20:08:32 +00:00
commit e3d1e60ed9
16 changed files with 827 additions and 230 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ if_chain! {
if let TyKind::Path(ref qp) = cast_ty.kind;
if match_qpath(qp, &["char"]);
if let ExprKind::Lit(ref lit) = expr.kind;
if let LitKind::Int(69, _) = lit.node;
if let LitKind::Int(69, LitIntType::Unsuffixed) = lit.node;
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.kind;
if name.as_str() == "x";
then {

View File

@ -1,10 +1,16 @@
// edition:2018
#![allow(redundant_semicolons, clippy::no_effect)]
#![feature(stmt_expr_attributes)]
#![feature(async_closure)]
#[rustfmt::skip]
fn main() {
#[clippy::author]
{
let x = 42i32;
let _t = 1f32;
-x;
};
#[clippy::author]
@ -12,4 +18,7 @@ fn main() {
let expr = String::new();
drop(expr)
};
#[clippy::author]
async move || {};
}

View File

@ -1,13 +1,19 @@
if_chain! {
if let ExprKind::Block(ref block) = expr.kind;
if block.stmts.len() == 2;
if let ExprKind::Block(ref block, ref label) = expr.kind;
if block.stmts.len() == 3;
if let StmtKind::Local(ref local) = block.stmts[0].kind;
if let Some(ref init) = local.init;
if let ExprKind::Lit(ref lit) = init.kind;
if let LitKind::Int(42, _) = lit.node;
if let LitKind::Int(42, LitIntType::Signed(IntTy::I32)) = lit.node;
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.kind;
if name.as_str() == "x";
if let StmtKind::Semi(ref e, _) = block.stmts[1].kind
if let StmtKind::Local(ref local1) = block.stmts[1].kind;
if let Some(ref init1) = local1.init;
if let ExprKind::Lit(ref lit1) = init1.kind;
if let LitKind::Float(_, LitFloatType::Suffixed(FloatTy::F32)) = lit1.node;
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local1.pat.kind;
if name1.as_str() == "_t";
if let StmtKind::Semi(ref e, _) = block.stmts[2].kind
if let ExprKind::Unary(UnOp::Neg, ref inner) = e.kind;
if let ExprKind::Path(ref path) = inner.kind;
if match_qpath(path, &["x"]);
@ -17,7 +23,7 @@ if_chain! {
}
}
if_chain! {
if let ExprKind::Block(ref block) = expr.kind;
if let ExprKind::Block(ref block, ref label) = expr.kind;
if block.stmts.len() == 1;
if let StmtKind::Local(ref local) = block.stmts[0].kind;
if let Some(ref init) = local.init;
@ -38,3 +44,21 @@ if_chain! {
// report your lint here
}
}
if_chain! {
if let ExprKind::Closure(CaptureBy::Value, ref fn_decl, ref body_id, _, None) = expr.kind
if let FnRetTy::DefaultReturn(_) = fn_decl.output
let body = cx.tcx.hir().body(body_id);
if let ExprKind::Call(ref func, ref args) = body.value.kind;
if let ExprKind::Path(ref path) = func.kind;
if matches!(path, QPath::LangItem(LangItem::FromGenerator, _));
if args.len() == 1;
if let ExprKind::Closure(CaptureBy::Value, ref fn_decl1, ref body_id1, _, Some(Movability::Static)) = args[0].kind
if let FnRetTy::DefaultReturn(_) = fn_decl1.output
let body1 = cx.tcx.hir().body(body_id1);
if let ExprKind::Block(ref block, ref label) = body1.value.kind;
if block.stmts.len() == 0;
if block.expr.is_none();
then {
// report your lint here
}
}

View File

@ -6,9 +6,9 @@ if_chain! {
if match_qpath(path, &["{{root}}", "std", "cmp", "min"]);
if args.len() == 2;
if let ExprKind::Lit(ref lit) = args[0].kind;
if let LitKind::Int(3, _) = lit.node;
if let LitKind::Int(3, LitIntType::Unsuffixed) = lit.node;
if let ExprKind::Lit(ref lit1) = args[1].kind;
if let LitKind::Int(4, _) = lit1.node;
if let LitKind::Int(4, LitIntType::Unsuffixed) = lit1.node;
if let PatKind::Wild = local.pat.kind;
then {
// report your lint here

View File

@ -1,8 +0,0 @@
#![feature(stmt_expr_attributes)]
fn main() {
#[clippy::author]
for y in 0..10 {
let z = y;
}
}

View File

@ -1,64 +0,0 @@
if_chain! {
if let ExprKind::DropTemps(ref expr) = expr.kind;
if let ExprKind::Match(ref expr1, ref arms, MatchSource::ForLoopDesugar) = expr.kind;
if let ExprKind::Call(ref func, ref args) = expr1.kind;
if let ExprKind::Path(ref path) = func.kind;
if matches!(path, QPath::LangItem(LangItem::IntoIterIntoIter, _));
if args.len() == 1;
if let ExprKind::Struct(ref path1, ref fields, None) = args[0].kind;
if matches!(path1, QPath::LangItem(LangItem::Range, _));
if fields.len() == 2;
// unimplemented: field checks
if arms.len() == 1;
if let ExprKind::Loop(ref body, ref label, LoopSource::ForLoop) = arms[0].body.kind;
if body.stmts.len() == 4;
if let StmtKind::Local(ref local) = body.stmts[0].kind;
if let PatKind::Binding(BindingAnnotation::Mutable, _, name, None) = local.pat.kind;
if name.as_str() == "__next";
if let StmtKind::Expr(ref e, _) = body.stmts[1].kind
if let ExprKind::Match(ref expr2, ref arms1, MatchSource::ForLoopDesugar) = e.kind;
if let ExprKind::Call(ref func1, ref args1) = expr2.kind;
if let ExprKind::Path(ref path2) = func1.kind;
if matches!(path2, QPath::LangItem(LangItem::IteratorNext, _));
if args1.len() == 1;
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Mut, ref inner) = args1[0].kind;
if let ExprKind::Path(ref path3) = inner.kind;
if match_qpath(path3, &["iter"]);
if arms1.len() == 2;
if let ExprKind::Assign(ref target, ref value, ref _span) = arms1[0].body.kind;
if let ExprKind::Path(ref path4) = target.kind;
if match_qpath(path4, &["__next"]);
if let ExprKind::Path(ref path5) = value.kind;
if match_qpath(path5, &["val"]);
if let PatKind::Struct(ref path6, ref fields1, false) = arms1[0].pat.kind;
if matches!(path6, QPath::LangItem(LangItem::OptionSome, _));
if fields1.len() == 1;
// unimplemented: field checks
if let ExprKind::Break(ref destination, None) = arms1[1].body.kind;
if let PatKind::Struct(ref path7, ref fields2, false) = arms1[1].pat.kind;
if matches!(path7, QPath::LangItem(LangItem::OptionNone, _));
if fields2.len() == 0;
// unimplemented: field checks
if let StmtKind::Local(ref local1) = body.stmts[2].kind;
if let Some(ref init) = local1.init;
if let ExprKind::Path(ref path8) = init.kind;
if match_qpath(path8, &["__next"]);
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local1.pat.kind;
if name1.as_str() == "y";
if let StmtKind::Expr(ref e1, _) = body.stmts[3].kind
if let ExprKind::Block(ref block) = e1.kind;
if block.stmts.len() == 1;
if let StmtKind::Local(ref local2) = block.stmts[0].kind;
if let Some(ref init1) = local2.init;
if let ExprKind::Path(ref path9) = init1.kind;
if match_qpath(path9, &["y"]);
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name2, None) = local2.pat.kind;
if name2.as_str() == "z";
if block.expr.is_none();
if body.expr.is_none();
if let PatKind::Binding(BindingAnnotation::Mutable, _, name3, None) = arms[0].pat.kind;
if name3.as_str() == "iter";
then {
// report your lint here
}
}

View File

@ -7,4 +7,11 @@ fn main() {
} else {
2 == 2;
};
let a = true;
#[clippy::author]
if let true = a {
} else {
};
}

View File

@ -1,32 +1,48 @@
if_chain! {
if let StmtKind::Local(ref local) = stmt.kind;
if let Some(ref init) = local.init;
if let ExprKind::If(ref cond, ref then, Some(ref else_)) = init.kind;
if let ExprKind::Block(ref block) = else_.kind;
if let Some(higher::If { cond: cond, then: then, r#else: else_expr}) = higher::If::hir(init)
if let ExprKind::Lit(ref lit) = cond.kind;
if let LitKind::Bool(true) = lit.node;
if let ExprKind::Block(ref block, ref label) = then.kind;
if block.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
if let ExprKind::Binary(ref op, ref left, ref right) = e.kind;
if BinOpKind::Eq == op.node;
if let ExprKind::Lit(ref lit) = left.kind;
if let LitKind::Int(2, _) = lit.node;
if let ExprKind::Lit(ref lit1) = right.kind;
if let LitKind::Int(2, _) = lit1.node;
if let ExprKind::Lit(ref lit1) = left.kind;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit1.node;
if let ExprKind::Lit(ref lit2) = right.kind;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit2.node;
if block.expr.is_none();
if let ExprKind::DropTemps(ref expr) = cond.kind;
if let ExprKind::Lit(ref lit2) = expr.kind;
if let LitKind::Bool(true) = lit2.node;
if let ExprKind::Block(ref block1) = then.kind;
if let ExprKind::Block(ref block1, ref label1) = else_expr.kind;
if block1.stmts.len() == 1;
if let StmtKind::Semi(ref e1, _) = block1.stmts[0].kind
if let ExprKind::Binary(ref op1, ref left1, ref right1) = e1.kind;
if BinOpKind::Eq == op1.node;
if let ExprKind::Lit(ref lit3) = left1.kind;
if let LitKind::Int(1, _) = lit3.node;
if let LitKind::Int(2, LitIntType::Unsuffixed) = lit3.node;
if let ExprKind::Lit(ref lit4) = right1.kind;
if let LitKind::Int(1, _) = lit4.node;
if let LitKind::Int(2, LitIntType::Unsuffixed) = lit4.node;
if block1.expr.is_none();
if let PatKind::Wild = local.pat.kind;
then {
// report your lint here
}
}
if_chain! {
if let Some(higher::IfLet { let_pat: let_pat, let_expr: let_expr, if_then: if_then, if_else: else_expr}) = higher::IfLet::hir(expr)
if let PatKind::Lit(ref lit_expr) = let_pat.kind
if let ExprKind::Lit(ref lit) = lit_expr.kind;
if let LitKind::Bool(true) = lit.node;
if let ExprKind::Path(ref path) = let_expr.kind;
if match_qpath(path, &["a"]);
if let ExprKind::Block(ref block, ref label) = if_then.kind;
if block.stmts.len() == 0;
if block.expr.is_none();
if let ExprKind::Block(ref block1, ref label1) = else_expr.kind;
if block1.stmts.len() == 0;
if block1.expr.is_none();
then {
// report your lint here
}
}

36
tests/ui/author/loop.rs Normal file
View File

@ -0,0 +1,36 @@
#![feature(stmt_expr_attributes)]
#![allow(clippy::never_loop, clippy::while_immutable_condition)]
fn main() {
#[clippy::author]
for y in 0..10 {
let z = y;
}
#[clippy::author]
for _ in 0..10 {
break;
}
#[clippy::author]
'label: for _ in 0..10 {
break 'label;
}
let a = true;
#[clippy::author]
while a {
break;
}
#[clippy::author]
while let true = a {
break;
}
#[clippy::author]
loop {
break;
}
}

112
tests/ui/author/loop.stdout Normal file
View File

@ -0,0 +1,112 @@
if_chain! {
if let ExprKind::DropTemps(ref expr) = expr.kind;
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = pat.kind;
if name.as_str() == "y";
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
if matches!(path, QPath::LangItem(LangItem::Range, _));
if fields.len() == 2;
if fields[0].ident.name.as_str() == "start"
if let ExprKind::Lit(ref lit) = fields[0].kind;
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
if fields[1].ident.name.as_str() == "end"
if let ExprKind::Lit(ref lit1) = fields[1].kind;
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
if let ExprKind::Block(ref block, ref label) = body.kind;
if block.stmts.len() == 1;
if let StmtKind::Local(ref local) = block.stmts[0].kind;
if let Some(ref init) = local.init;
if let ExprKind::Path(ref path1) = init.kind;
if match_qpath(path1, &["y"]);
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local.pat.kind;
if name1.as_str() == "z";
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let ExprKind::DropTemps(ref expr) = expr.kind;
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
if let PatKind::Wild = pat.kind;
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
if matches!(path, QPath::LangItem(LangItem::Range, _));
if fields.len() == 2;
if fields[0].ident.name.as_str() == "start"
if let ExprKind::Lit(ref lit) = fields[0].kind;
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
if fields[1].ident.name.as_str() == "end"
if let ExprKind::Lit(ref lit1) = fields[1].kind;
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
if let ExprKind::Block(ref block, ref label) = body.kind;
if block.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
if let ExprKind::Break(ref destination, None) = e.kind;
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let ExprKind::DropTemps(ref expr) = expr.kind;
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
if let PatKind::Wild = pat.kind;
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
if matches!(path, QPath::LangItem(LangItem::Range, _));
if fields.len() == 2;
if fields[0].ident.name.as_str() == "start"
if let ExprKind::Lit(ref lit) = fields[0].kind;
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
if fields[1].ident.name.as_str() == "end"
if let ExprKind::Lit(ref lit1) = fields[1].kind;
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
if let ExprKind::Block(ref block, ref label) = body.kind;
if block.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
if let ExprKind::Break(ref destination, None) = e.kind;
if let Some(ref label1) = destination.label
if label_name.ident.name.as_str() == "'label";
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let Some(higher::While { condition: condition, body: body }) = higher::While::hir(expr)
if let ExprKind::Path(ref path) = condition.kind;
if match_qpath(path, &["a"]);
if let ExprKind::Block(ref block, ref label) = body.kind;
if block.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
if let ExprKind::Break(ref destination, None) = e.kind;
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let Some(higher::WhileLet { let_pat: let_pat, let_expr: let_expr, if_then: if_then }) = higher::WhileLet::hir(expr)
if let PatKind::Lit(ref lit_expr) = let_pat.kind
if let ExprKind::Lit(ref lit) = lit_expr.kind;
if let LitKind::Bool(true) = lit.node;
if let ExprKind::Path(ref path) = let_expr.kind;
if match_qpath(path, &["a"]);
if let ExprKind::Block(ref block, ref label) = if_then.kind;
if block.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
if let ExprKind::Break(ref destination, None) = e.kind;
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let ExprKind::Loop(ref body, ref label, LoopSource::Loop) = expr.kind;
if body.stmts.len() == 1;
if let StmtKind::Semi(ref e, _) = body.stmts[0].kind
if let ExprKind::Break(ref destination, None) = e.kind;
if body.expr.is_none();
then {
// report your lint here
}
}

View File

@ -3,19 +3,19 @@ if_chain! {
if let Some(ref init) = local.init;
if let ExprKind::Match(ref expr, ref arms, MatchSource::Normal) = init.kind;
if let ExprKind::Lit(ref lit) = expr.kind;
if let LitKind::Int(42, _) = lit.node;
if let LitKind::Int(42, LitIntType::Unsuffixed) = lit.node;
if arms.len() == 3;
if let ExprKind::Lit(ref lit1) = arms[0].body.kind;
if let LitKind::Int(5, _) = lit1.node;
if let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node;
if let PatKind::Lit(ref lit_expr) = arms[0].pat.kind
if let ExprKind::Lit(ref lit2) = lit_expr.kind;
if let LitKind::Int(16, _) = lit2.node;
if let ExprKind::Block(ref block) = arms[1].body.kind;
if let LitKind::Int(16, LitIntType::Unsuffixed) = lit2.node;
if let ExprKind::Block(ref block, ref label) = arms[1].body.kind;
if block.stmts.len() == 1;
if let StmtKind::Local(ref local1) = block.stmts[0].kind;
if let Some(ref init1) = local1.init;
if let ExprKind::Lit(ref lit3) = init1.kind;
if let LitKind::Int(3, _) = lit3.node;
if let LitKind::Int(3, LitIntType::Unsuffixed) = lit3.node;
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local1.pat.kind;
if name.as_str() == "x";
if let Some(trailing_expr) = &block.expr;
@ -23,9 +23,9 @@ if_chain! {
if match_qpath(path, &["x"]);
if let PatKind::Lit(ref lit_expr1) = arms[1].pat.kind
if let ExprKind::Lit(ref lit4) = lit_expr1.kind;
if let LitKind::Int(17, _) = lit4.node;
if let LitKind::Int(17, LitIntType::Unsuffixed) = lit4.node;
if let ExprKind::Lit(ref lit5) = arms[2].body.kind;
if let LitKind::Int(1, _) = lit5.node;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit5.node;
if let PatKind::Wild = arms[2].pat.kind;
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local.pat.kind;
if name1.as_str() == "a";

View File

@ -0,0 +1,5 @@
#[allow(clippy::no_effect)]
fn main() {
#[clippy::author]
[1_u8; 5];
}

View File

@ -0,0 +1,10 @@
if_chain! {
if let ExprKind::Repeat(ref value, ref length) = expr.kind;
if let ExprKind::Lit(ref lit) = value.kind;
if let LitKind::Int(1, LitIntType::Unsigned(UintTy::U8)) = lit.node;
if let ExprKind::Lit(ref lit1) = length.value.kind;
if let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node;
then {
// report your lint here
}
}

40
tests/ui/author/struct.rs Normal file
View File

@ -0,0 +1,40 @@
#[allow(clippy::unnecessary_operation, clippy::single_match)]
fn main() {
struct Test {
field: u32,
}
#[clippy::author]
Test {
field: if true { 1 } else { 0 },
};
let test = Test { field: 1 };
match test {
#[clippy::author]
Test { field: 1 } => {},
_ => {},
}
struct TestTuple(u32);
let test_tuple = TestTuple(1);
match test_tuple {
#[clippy::author]
TestTuple(1) => {},
_ => {},
}
struct TestMethodCall(u32);
impl TestMethodCall {
fn test(&self) {}
}
let test_method_call = TestMethodCall(1);
#[clippy::author]
test_method_call.test();
}

View File

@ -0,0 +1,61 @@
if_chain! {
if let ExprKind::Struct(ref path, ref fields, None) = expr.kind;
if match_qpath(path, &["Test"]);
if fields.len() == 1;
if fields[0].ident.name.as_str() == "field"
if let Some(higher::If { cond: cond, then: then, r#else: else_expr}) = higher::If::hir(fields[0])
if let ExprKind::Lit(ref lit) = cond.kind;
if let LitKind::Bool(true) = lit.node;
if let ExprKind::Block(ref block, ref label) = then.kind;
if block.stmts.len() == 0;
if let Some(trailing_expr) = &block.expr;
if let ExprKind::Lit(ref lit1) = trailing_expr.kind;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit1.node;
if let ExprKind::Block(ref block1, ref label1) = else_expr.kind;
if block1.stmts.len() == 0;
if let Some(trailing_expr1) = &block1.expr;
if let ExprKind::Lit(ref lit2) = trailing_expr1.kind;
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit2.node;
then {
// report your lint here
}
}
if_chain! {
if let PatKind::Struct(ref path, ref fields, false) = arm.kind;
if match_qpath(path, &["Test"]);
if fields.len() == 1;
if fields[0].ident.name.as_str() == "field"
if let PatKind::Lit(ref lit_expr) = fields[0].kind
if let ExprKind::Lit(ref lit) = lit_expr.kind;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit.node;
if let ExprKind::Block(ref block, ref label) = lit_expr.kind;
if block.stmts.len() == 0;
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let PatKind::TupleStruct(ref path, ref fields, None) = arm.kind;
if match_qpath(path, &["TestTuple"]);
if fields.len() == 1;
if let PatKind::Lit(ref lit_expr) = fields[0].kind
if let ExprKind::Lit(ref lit) = lit_expr.kind;
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit.node;
if let ExprKind::Block(ref block, ref label) = lit_expr.kind;
if block.stmts.len() == 0;
if block.expr.is_none();
then {
// report your lint here
}
}
if_chain! {
if let ExprKind::MethodCall(ref method_name, ref args, _) = expr.kind;
if method_name.ident.name.as_str() == test;
if args.len() == 1;
if let ExprKind::Path(ref path) = args[0].kind;
if match_qpath(path, &["test_method_call"]);
then {
// report your lint here
}
}