Skip to content

Extend author lint #7894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
611 changes: 480 additions & 131 deletions clippy_lints/src/utils/author.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/ui/author.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/author/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
// 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]
{
let expr = String::new();
drop(expr)
};

#[clippy::author]
async move || {};
}
34 changes: 29 additions & 5 deletions tests/ui/author/blocks.stdout
Original file line number Diff line number Diff line change
@@ -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"]);
Expand All @@ -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;
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions tests/ui/author/call.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions tests/ui/author/for_loop.rs

This file was deleted.

64 changes: 0 additions & 64 deletions tests/ui/author/for_loop.stdout

This file was deleted.

7 changes: 7 additions & 0 deletions tests/ui/author/if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ fn main() {
} else {
2 == 2;
};

let a = true;

#[clippy::author]
if let true = a {
} else {
};
}
40 changes: 28 additions & 12 deletions tests/ui/author/if.stdout
Original file line number Diff line number Diff line change
@@ -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 changes: 36 additions & 0 deletions tests/ui/author/loop.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 112 additions & 0 deletions tests/ui/author/loop.stdout
Original file line number Diff line number Diff line change
@@ -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
}
}
Loading