Skip to content

Commit 0ab3f17

Browse files
committed
Auto merge of #4113 - rust-lang:author, r=flip1995
Fix `Block` dump in author lint changelog: The `#[clippy::author]` attribute now emits correct pattern code for block expressions
2 parents d2f5122 + 3908d86 commit 0ab3f17

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

clippy_lints/src/utils/author.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use crate::utils::{get_attr, higher};
55
use rustc::hir;
66
use rustc::hir::intravisit::{NestedVisitorMap, Visitor};
7-
use rustc::hir::{BindingAnnotation, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
7+
use rustc::hir::{BindingAnnotation, Block, Expr, ExprKind, Pat, PatKind, QPath, Stmt, StmtKind, TyKind};
88
use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
99
use rustc::session::Session;
1010
use rustc::{declare_lint_pass, declare_tool_lint};
@@ -511,6 +511,17 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
511511
}
512512
}
513513

514+
fn visit_block(&mut self, block: &Block) {
515+
let trailing_pat = self.next("trailing_expr");
516+
println!(" if let Some({}) = &{}.expr;", trailing_pat, self.current);
517+
println!(" if {}.stmts.len() == {};", self.current, block.stmts.len());
518+
let current = self.current.clone();
519+
for (i, stmt) in block.stmts.iter().enumerate() {
520+
self.current = format!("{}.stmts[{}]", current, i);
521+
self.visit_stmt(stmt);
522+
}
523+
}
524+
514525
#[allow(clippy::too_many_lines)]
515526
fn visit_pat(&mut self, pat: &Pat) {
516527
print!(" if let PatKind::");

tests/ui/author/blocks.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(stmt_expr_attributes)]
2+
3+
#[rustfmt::skip]
4+
fn main() {
5+
#[clippy::author]
6+
{
7+
;;;;
8+
}
9+
}
10+
11+
#[clippy::author]
12+
fn foo() {
13+
let x = 42i32;
14+
-x;
15+
}

tests/ui/author/blocks.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: statement with no effect
2+
--> $DIR/blocks.rs:14:5
3+
|
4+
LL | -x;
5+
| ^^^
6+
|
7+
= note: `-D clippy::no-effect` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

tests/ui/author/blocks.stdout

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
if_chain! {
2+
if let ExprKind::Block(ref block) = expr.node;
3+
if let Some(trailing_expr) = &block.expr;
4+
if block.stmts.len() == 0;
5+
then {
6+
// report your lint here
7+
}
8+
}
9+
if_chain! {
10+
then {
11+
// report your lint here
12+
}
13+
}

tests/ui/author/for_loop.stdout

+9-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ if_chain! {
1111
// unimplemented: field checks
1212
if arms.len() == 1;
1313
if let ExprKind::Loop(ref body, ref label, LoopSource::ForLoop) = arms[0].body.node;
14-
if let StmtKind::Local(ref local) = body.node;
14+
if let Some(trailing_expr) = &body.expr;
15+
if body.stmts.len() == 4;
16+
if let StmtKind::Local(ref local) = body.stmts[0].node;
1517
if let PatKind::Binding(BindingAnnotation::Mutable, _, name, None) = local.pat.node;
1618
if name.node.as_str() == "__next";
17-
if let StmtKind::Expr(ref e, _) = local.pat.node
19+
if let StmtKind::Expr(ref e, _) = body.stmts[1].node
1820
if let ExprKind::Match(ref expr2, ref arms1, MatchSource::ForLoopDesugar) = e.node;
1921
if let ExprKind::Call(ref func1, ref args1) = expr2.node;
2022
if let ExprKind::Path(ref path2) = func1.node;
@@ -38,15 +40,17 @@ if_chain! {
3840
if arms1[1].pats.len() == 1;
3941
if let PatKind::Path(ref path7) = arms1[1].pats[0].node;
4042
if match_qpath(path7, &["{{root}}", "std", "option", "Option", "None"]);
41-
if let StmtKind::Local(ref local1) = path7.node;
43+
if let StmtKind::Local(ref local1) = body.stmts[2].node;
4244
if let Some(ref init) = local1.init;
4345
if let ExprKind::Path(ref path8) = init.node;
4446
if match_qpath(path8, &["__next"]);
4547
if let PatKind::Binding(BindingAnnotation::Unannotated, _, name1, None) = local1.pat.node;
4648
if name1.node.as_str() == "y";
47-
if let StmtKind::Expr(ref e1, _) = local1.pat.node
49+
if let StmtKind::Expr(ref e1, _) = body.stmts[3].node
4850
if let ExprKind::Block(ref block) = e1.node;
49-
if let StmtKind::Local(ref local2) = block.node;
51+
if let Some(trailing_expr1) = &block.expr;
52+
if block.stmts.len() == 1;
53+
if let StmtKind::Local(ref local2) = block.stmts[0].node;
5054
if let Some(ref init1) = local2.init;
5155
if let ExprKind::Path(ref path9) = init1.node;
5256
if match_qpath(path9, &["y"]);

tests/ui/author/if.stdout

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ if_chain! {
33
if let Some(ref init) = local.init;
44
if let Some((ref cond, ref then, Some(else_))) = higher::if_block(&init);
55
if let ExprKind::Block(ref block) = else_.node;
6-
if let StmtKind::Semi(ref e, _) = block.node
6+
if let Some(trailing_expr) = &block.expr;
7+
if block.stmts.len() == 1;
8+
if let StmtKind::Semi(ref e, _) = block.stmts[0].node
79
if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
810
if BinOpKind::Eq == op.node;
911
if let ExprKind::Lit(ref lit) = left.node;
@@ -13,7 +15,9 @@ if_chain! {
1315
if let ExprKind::Lit(ref lit2) = cond.node;
1416
if let LitKind::Bool(true) = lit2.node;
1517
if let ExprKind::Block(ref block1) = then.node;
16-
if let StmtKind::Semi(ref e1, _) = block1.node
18+
if let Some(trailing_expr1) = &block1.expr;
19+
if block1.stmts.len() == 1;
20+
if let StmtKind::Semi(ref e1, _) = block1.stmts[0].node
1721
if let ExprKind::Binary(ref op1, ref left1, ref right1) = e1.node;
1822
if BinOpKind::Eq == op1.node;
1923
if let ExprKind::Lit(ref lit3) = left1.node;

0 commit comments

Comments
 (0)