Skip to content

Commit e3d1e60

Browse files
committed
Auto merge of rust-lang#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
2 parents 445c83f + c96cd35 commit e3d1e60

File tree

16 files changed

+827
-230
lines changed

16 files changed

+827
-230
lines changed

clippy_lints/src/utils/author.rs

Lines changed: 480 additions & 131 deletions
Large diffs are not rendered by default.

tests/ui/author.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ if_chain! {
55
if let TyKind::Path(ref qp) = cast_ty.kind;
66
if match_qpath(qp, &["char"]);
77
if let ExprKind::Lit(ref lit) = expr.kind;
8-
if let LitKind::Int(69, _) = lit.node;
8+
if let LitKind::Int(69, LitIntType::Unsuffixed) = lit.node;
99
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.kind;
1010
if name.as_str() == "x";
1111
then {

tests/ui/author/blocks.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
// edition:2018
2+
13
#![allow(redundant_semicolons, clippy::no_effect)]
4+
#![feature(stmt_expr_attributes)]
5+
#![feature(async_closure)]
26

37
#[rustfmt::skip]
48
fn main() {
59
#[clippy::author]
610
{
711
let x = 42i32;
12+
let _t = 1f32;
13+
814
-x;
915
};
1016
#[clippy::author]
1117
{
1218
let expr = String::new();
1319
drop(expr)
1420
};
21+
22+
#[clippy::author]
23+
async move || {};
1524
}

tests/ui/author/blocks.stdout

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
if_chain! {
2-
if let ExprKind::Block(ref block) = expr.kind;
3-
if block.stmts.len() == 2;
2+
if let ExprKind::Block(ref block, ref label) = expr.kind;
3+
if block.stmts.len() == 3;
44
if let StmtKind::Local(ref local) = block.stmts[0].kind;
55
if let Some(ref init) = local.init;
66
if let ExprKind::Lit(ref lit) = init.kind;
7-
if let LitKind::Int(42, _) = lit.node;
7+
if let LitKind::Int(42, LitIntType::Signed(IntTy::I32)) = lit.node;
88
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = local.pat.kind;
99
if name.as_str() == "x";
10-
if let StmtKind::Semi(ref e, _) = block.stmts[1].kind
10+
if let StmtKind::Local(ref local1) = block.stmts[1].kind;
11+
if let Some(ref init1) = local1.init;
12+
if let ExprKind::Lit(ref lit1) = init1.kind;
13+
if let LitKind::Float(_, LitFloatType::Suffixed(FloatTy::F32)) = lit1.node;
14+
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local1.pat.kind;
15+
if name1.as_str() == "_t";
16+
if let StmtKind::Semi(ref e, _) = block.stmts[2].kind
1117
if let ExprKind::Unary(UnOp::Neg, ref inner) = e.kind;
1218
if let ExprKind::Path(ref path) = inner.kind;
1319
if match_qpath(path, &["x"]);
@@ -17,7 +23,7 @@ if_chain! {
1723
}
1824
}
1925
if_chain! {
20-
if let ExprKind::Block(ref block) = expr.kind;
26+
if let ExprKind::Block(ref block, ref label) = expr.kind;
2127
if block.stmts.len() == 1;
2228
if let StmtKind::Local(ref local) = block.stmts[0].kind;
2329
if let Some(ref init) = local.init;
@@ -38,3 +44,21 @@ if_chain! {
3844
// report your lint here
3945
}
4046
}
47+
if_chain! {
48+
if let ExprKind::Closure(CaptureBy::Value, ref fn_decl, ref body_id, _, None) = expr.kind
49+
if let FnRetTy::DefaultReturn(_) = fn_decl.output
50+
let body = cx.tcx.hir().body(body_id);
51+
if let ExprKind::Call(ref func, ref args) = body.value.kind;
52+
if let ExprKind::Path(ref path) = func.kind;
53+
if matches!(path, QPath::LangItem(LangItem::FromGenerator, _));
54+
if args.len() == 1;
55+
if let ExprKind::Closure(CaptureBy::Value, ref fn_decl1, ref body_id1, _, Some(Movability::Static)) = args[0].kind
56+
if let FnRetTy::DefaultReturn(_) = fn_decl1.output
57+
let body1 = cx.tcx.hir().body(body_id1);
58+
if let ExprKind::Block(ref block, ref label) = body1.value.kind;
59+
if block.stmts.len() == 0;
60+
if block.expr.is_none();
61+
then {
62+
// report your lint here
63+
}
64+
}

tests/ui/author/call.stdout

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ if_chain! {
66
if match_qpath(path, &["{{root}}", "std", "cmp", "min"]);
77
if args.len() == 2;
88
if let ExprKind::Lit(ref lit) = args[0].kind;
9-
if let LitKind::Int(3, _) = lit.node;
9+
if let LitKind::Int(3, LitIntType::Unsuffixed) = lit.node;
1010
if let ExprKind::Lit(ref lit1) = args[1].kind;
11-
if let LitKind::Int(4, _) = lit1.node;
11+
if let LitKind::Int(4, LitIntType::Unsuffixed) = lit1.node;
1212
if let PatKind::Wild = local.pat.kind;
1313
then {
1414
// report your lint here

tests/ui/author/for_loop.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/ui/author/for_loop.stdout

Lines changed: 0 additions & 64 deletions
This file was deleted.

tests/ui/author/if.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,11 @@ fn main() {
77
} else {
88
2 == 2;
99
};
10+
11+
let a = true;
12+
13+
#[clippy::author]
14+
if let true = a {
15+
} else {
16+
};
1017
}

tests/ui/author/if.stdout

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
if_chain! {
22
if let StmtKind::Local(ref local) = stmt.kind;
33
if let Some(ref init) = local.init;
4-
if let ExprKind::If(ref cond, ref then, Some(ref else_)) = init.kind;
5-
if let ExprKind::Block(ref block) = else_.kind;
4+
if let Some(higher::If { cond: cond, then: then, r#else: else_expr}) = higher::If::hir(init)
5+
if let ExprKind::Lit(ref lit) = cond.kind;
6+
if let LitKind::Bool(true) = lit.node;
7+
if let ExprKind::Block(ref block, ref label) = then.kind;
68
if block.stmts.len() == 1;
79
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
810
if let ExprKind::Binary(ref op, ref left, ref right) = e.kind;
911
if BinOpKind::Eq == op.node;
10-
if let ExprKind::Lit(ref lit) = left.kind;
11-
if let LitKind::Int(2, _) = lit.node;
12-
if let ExprKind::Lit(ref lit1) = right.kind;
13-
if let LitKind::Int(2, _) = lit1.node;
12+
if let ExprKind::Lit(ref lit1) = left.kind;
13+
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit1.node;
14+
if let ExprKind::Lit(ref lit2) = right.kind;
15+
if let LitKind::Int(1, LitIntType::Unsuffixed) = lit2.node;
1416
if block.expr.is_none();
15-
if let ExprKind::DropTemps(ref expr) = cond.kind;
16-
if let ExprKind::Lit(ref lit2) = expr.kind;
17-
if let LitKind::Bool(true) = lit2.node;
18-
if let ExprKind::Block(ref block1) = then.kind;
17+
if let ExprKind::Block(ref block1, ref label1) = else_expr.kind;
1918
if block1.stmts.len() == 1;
2019
if let StmtKind::Semi(ref e1, _) = block1.stmts[0].kind
2120
if let ExprKind::Binary(ref op1, ref left1, ref right1) = e1.kind;
2221
if BinOpKind::Eq == op1.node;
2322
if let ExprKind::Lit(ref lit3) = left1.kind;
24-
if let LitKind::Int(1, _) = lit3.node;
23+
if let LitKind::Int(2, LitIntType::Unsuffixed) = lit3.node;
2524
if let ExprKind::Lit(ref lit4) = right1.kind;
26-
if let LitKind::Int(1, _) = lit4.node;
25+
if let LitKind::Int(2, LitIntType::Unsuffixed) = lit4.node;
2726
if block1.expr.is_none();
2827
if let PatKind::Wild = local.pat.kind;
2928
then {
3029
// report your lint here
3130
}
3231
}
32+
if_chain! {
33+
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)
34+
if let PatKind::Lit(ref lit_expr) = let_pat.kind
35+
if let ExprKind::Lit(ref lit) = lit_expr.kind;
36+
if let LitKind::Bool(true) = lit.node;
37+
if let ExprKind::Path(ref path) = let_expr.kind;
38+
if match_qpath(path, &["a"]);
39+
if let ExprKind::Block(ref block, ref label) = if_then.kind;
40+
if block.stmts.len() == 0;
41+
if block.expr.is_none();
42+
if let ExprKind::Block(ref block1, ref label1) = else_expr.kind;
43+
if block1.stmts.len() == 0;
44+
if block1.expr.is_none();
45+
then {
46+
// report your lint here
47+
}
48+
}

tests/ui/author/loop.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(stmt_expr_attributes)]
2+
#![allow(clippy::never_loop, clippy::while_immutable_condition)]
3+
4+
fn main() {
5+
#[clippy::author]
6+
for y in 0..10 {
7+
let z = y;
8+
}
9+
10+
#[clippy::author]
11+
for _ in 0..10 {
12+
break;
13+
}
14+
15+
#[clippy::author]
16+
'label: for _ in 0..10 {
17+
break 'label;
18+
}
19+
20+
let a = true;
21+
22+
#[clippy::author]
23+
while a {
24+
break;
25+
}
26+
27+
#[clippy::author]
28+
while let true = a {
29+
break;
30+
}
31+
32+
#[clippy::author]
33+
loop {
34+
break;
35+
}
36+
}

tests/ui/author/loop.stdout

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
if_chain! {
2+
if let ExprKind::DropTemps(ref expr) = expr.kind;
3+
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
4+
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name, None) = pat.kind;
5+
if name.as_str() == "y";
6+
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
7+
if matches!(path, QPath::LangItem(LangItem::Range, _));
8+
if fields.len() == 2;
9+
if fields[0].ident.name.as_str() == "start"
10+
if let ExprKind::Lit(ref lit) = fields[0].kind;
11+
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
12+
if fields[1].ident.name.as_str() == "end"
13+
if let ExprKind::Lit(ref lit1) = fields[1].kind;
14+
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
15+
if let ExprKind::Block(ref block, ref label) = body.kind;
16+
if block.stmts.len() == 1;
17+
if let StmtKind::Local(ref local) = block.stmts[0].kind;
18+
if let Some(ref init) = local.init;
19+
if let ExprKind::Path(ref path1) = init.kind;
20+
if match_qpath(path1, &["y"]);
21+
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local.pat.kind;
22+
if name1.as_str() == "z";
23+
if block.expr.is_none();
24+
then {
25+
// report your lint here
26+
}
27+
}
28+
if_chain! {
29+
if let ExprKind::DropTemps(ref expr) = expr.kind;
30+
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
31+
if let PatKind::Wild = pat.kind;
32+
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
33+
if matches!(path, QPath::LangItem(LangItem::Range, _));
34+
if fields.len() == 2;
35+
if fields[0].ident.name.as_str() == "start"
36+
if let ExprKind::Lit(ref lit) = fields[0].kind;
37+
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
38+
if fields[1].ident.name.as_str() == "end"
39+
if let ExprKind::Lit(ref lit1) = fields[1].kind;
40+
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
41+
if let ExprKind::Block(ref block, ref label) = body.kind;
42+
if block.stmts.len() == 1;
43+
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
44+
if let ExprKind::Break(ref destination, None) = e.kind;
45+
if block.expr.is_none();
46+
then {
47+
// report your lint here
48+
}
49+
}
50+
if_chain! {
51+
if let ExprKind::DropTemps(ref expr) = expr.kind;
52+
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, ..}) = higher::ForLoop::hir(expr)
53+
if let PatKind::Wild = pat.kind;
54+
if let ExprKind::Struct(ref path, ref fields, None) = arg.kind;
55+
if matches!(path, QPath::LangItem(LangItem::Range, _));
56+
if fields.len() == 2;
57+
if fields[0].ident.name.as_str() == "start"
58+
if let ExprKind::Lit(ref lit) = fields[0].kind;
59+
if let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node;
60+
if fields[1].ident.name.as_str() == "end"
61+
if let ExprKind::Lit(ref lit1) = fields[1].kind;
62+
if let LitKind::Int(10, LitIntType::Unsuffixed) = lit1.node;
63+
if let ExprKind::Block(ref block, ref label) = body.kind;
64+
if block.stmts.len() == 1;
65+
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
66+
if let ExprKind::Break(ref destination, None) = e.kind;
67+
if let Some(ref label1) = destination.label
68+
if label_name.ident.name.as_str() == "'label";
69+
if block.expr.is_none();
70+
then {
71+
// report your lint here
72+
}
73+
}
74+
if_chain! {
75+
if let Some(higher::While { condition: condition, body: body }) = higher::While::hir(expr)
76+
if let ExprKind::Path(ref path) = condition.kind;
77+
if match_qpath(path, &["a"]);
78+
if let ExprKind::Block(ref block, ref label) = body.kind;
79+
if block.stmts.len() == 1;
80+
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
81+
if let ExprKind::Break(ref destination, None) = e.kind;
82+
if block.expr.is_none();
83+
then {
84+
// report your lint here
85+
}
86+
}
87+
if_chain! {
88+
if let Some(higher::WhileLet { let_pat: let_pat, let_expr: let_expr, if_then: if_then }) = higher::WhileLet::hir(expr)
89+
if let PatKind::Lit(ref lit_expr) = let_pat.kind
90+
if let ExprKind::Lit(ref lit) = lit_expr.kind;
91+
if let LitKind::Bool(true) = lit.node;
92+
if let ExprKind::Path(ref path) = let_expr.kind;
93+
if match_qpath(path, &["a"]);
94+
if let ExprKind::Block(ref block, ref label) = if_then.kind;
95+
if block.stmts.len() == 1;
96+
if let StmtKind::Semi(ref e, _) = block.stmts[0].kind
97+
if let ExprKind::Break(ref destination, None) = e.kind;
98+
if block.expr.is_none();
99+
then {
100+
// report your lint here
101+
}
102+
}
103+
if_chain! {
104+
if let ExprKind::Loop(ref body, ref label, LoopSource::Loop) = expr.kind;
105+
if body.stmts.len() == 1;
106+
if let StmtKind::Semi(ref e, _) = body.stmts[0].kind
107+
if let ExprKind::Break(ref destination, None) = e.kind;
108+
if body.expr.is_none();
109+
then {
110+
// report your lint here
111+
}
112+
}

0 commit comments

Comments
 (0)