Skip to content

Commit c40359d

Browse files
committed
Auto merge of #11664 - koka831:fix/11134, r=blyxyas
Fix/11134 Fix #11134 Hir of `qpath` will be `TypeRelative(Ty { kind: Path(LangItem...` when a closure contains macro (e.g. #11651) and #11134, it causes panic. This PR avoids panicking and emitting incomplete path string when `qpath` contains `LangItem`. changelog: none
2 parents ff00e9c + 9fc717d commit c40359d

File tree

5 files changed

+110
-8
lines changed

5 files changed

+110
-8
lines changed

clippy_lints/src/utils/author.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
268268
fn qpath(&self, qpath: &Binding<&QPath<'_>>) {
269269
if let QPath::LangItem(lang_item, ..) = *qpath.value {
270270
chain!(self, "matches!({qpath}, QPath::LangItem(LangItem::{lang_item:?}, _))");
271-
} else {
272-
chain!(self, "match_qpath({qpath}, &[{}])", path_to_string(qpath.value));
271+
} else if let Ok(path) = path_to_string(qpath.value) {
272+
chain!(self, "match_qpath({qpath}, &[{}])", path);
273273
}
274274
}
275275

@@ -738,8 +738,8 @@ fn has_attr(cx: &LateContext<'_>, hir_id: hir::HirId) -> bool {
738738
get_attr(cx.sess(), attrs, "author").count() > 0
739739
}
740740

741-
fn path_to_string(path: &QPath<'_>) -> String {
742-
fn inner(s: &mut String, path: &QPath<'_>) {
741+
fn path_to_string(path: &QPath<'_>) -> Result<String, ()> {
742+
fn inner(s: &mut String, path: &QPath<'_>) -> Result<(), ()> {
743743
match *path {
744744
QPath::Resolved(_, path) => {
745745
for (i, segment) in path.segments.iter().enumerate() {
@@ -751,16 +751,18 @@ fn path_to_string(path: &QPath<'_>) -> String {
751751
},
752752
QPath::TypeRelative(ty, segment) => match &ty.kind {
753753
hir::TyKind::Path(inner_path) => {
754-
inner(s, inner_path);
754+
inner(s, inner_path)?;
755755
*s += ", ";
756756
write!(s, "{:?}", segment.ident.as_str()).unwrap();
757757
},
758758
other => write!(s, "/* unimplemented: {other:?}*/").unwrap(),
759759
},
760-
QPath::LangItem(..) => panic!("path_to_string: called for lang item qpath"),
760+
QPath::LangItem(..) => return Err(()),
761761
}
762+
763+
Ok(())
762764
}
763765
let mut s = String::new();
764-
inner(&mut s, path);
765-
s
766+
inner(&mut s, path)?;
767+
Ok(s)
766768
}

tests/ui/author/macro_in_closure.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
#[clippy::author]
3+
let print_text = |x| println!("{}", x);
4+
print_text("hello");
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
if let StmtKind::Local(local) = stmt.kind
2+
&& let Some(init) = local.init
3+
&& let ExprKind::Closure(CaptureBy::Ref, fn_decl, body_id, _, None) = init.kind
4+
&& let FnRetTy::DefaultReturn(_) = fn_decl.output
5+
&& expr = &cx.tcx.hir().body(body_id).value
6+
&& let ExprKind::Block(block, None) = expr.kind
7+
&& block.stmts.len() == 1
8+
&& let StmtKind::Semi(e) = block.stmts[0].kind
9+
&& let ExprKind::Call(func, args) = e.kind
10+
&& let ExprKind::Path(ref qpath) = func.kind
11+
&& match_qpath(qpath, &["$crate", "io", "_print"])
12+
&& args.len() == 1
13+
&& let ExprKind::Call(func1, args1) = args[0].kind
14+
&& let ExprKind::Path(ref qpath1) = func1.kind
15+
&& args1.len() == 2
16+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = args1[0].kind
17+
&& let ExprKind::Array(elements) = inner.kind
18+
&& elements.len() == 2
19+
&& let ExprKind::Lit(ref lit) = elements[0].kind
20+
&& let LitKind::Str(s, _) = lit.node
21+
&& s.as_str() == ""
22+
&& let ExprKind::Lit(ref lit1) = elements[1].kind
23+
&& let LitKind::Str(s1, _) = lit1.node
24+
&& s1.as_str() == "\n"
25+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner1) = args1[1].kind
26+
&& let ExprKind::Array(elements1) = inner1.kind
27+
&& elements1.len() == 1
28+
&& let ExprKind::Call(func2, args2) = elements1[0].kind
29+
&& let ExprKind::Path(ref qpath2) = func2.kind
30+
&& args2.len() == 1
31+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner2) = args2[0].kind
32+
&& let ExprKind::Path(ref qpath3) = inner2.kind
33+
&& match_qpath(qpath3, &["x"])
34+
&& block.expr.is_none()
35+
&& let PatKind::Binding(BindingAnnotation::NONE, _, name, None) = local.pat.kind
36+
&& name.as_str() == "print_text"
37+
{
38+
// report your lint here
39+
}

tests/ui/author/macro_in_loop.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(stmt_expr_attributes)]
2+
3+
fn main() {
4+
#[clippy::author]
5+
for i in 0..1 {
6+
println!("{}", i);
7+
}
8+
}

tests/ui/author/macro_in_loop.stdout

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
if let Some(higher::ForLoop { pat: pat, arg: arg, body: body, .. }) = higher::ForLoop::hir(expr)
2+
&& let PatKind::Binding(BindingAnnotation::NONE, _, name, None) = pat.kind
3+
&& name.as_str() == "i"
4+
&& let ExprKind::Struct(qpath, fields, None) = arg.kind
5+
&& matches!(qpath, QPath::LangItem(LangItem::Range, _))
6+
&& fields.len() == 2
7+
&& fields[0].ident.as_str() == "start"
8+
&& let ExprKind::Lit(ref lit) = fields[0].expr.kind
9+
&& let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node
10+
&& fields[1].ident.as_str() == "end"
11+
&& let ExprKind::Lit(ref lit1) = fields[1].expr.kind
12+
&& let LitKind::Int(1, LitIntType::Unsuffixed) = lit1.node
13+
&& let ExprKind::Block(block, None) = body.kind
14+
&& block.stmts.len() == 1
15+
&& let StmtKind::Semi(e) = block.stmts[0].kind
16+
&& let ExprKind::Block(block1, None) = e.kind
17+
&& block1.stmts.len() == 1
18+
&& let StmtKind::Semi(e1) = block1.stmts[0].kind
19+
&& let ExprKind::Call(func, args) = e1.kind
20+
&& let ExprKind::Path(ref qpath1) = func.kind
21+
&& match_qpath(qpath1, &["$crate", "io", "_print"])
22+
&& args.len() == 1
23+
&& let ExprKind::Call(func1, args1) = args[0].kind
24+
&& let ExprKind::Path(ref qpath2) = func1.kind
25+
&& args1.len() == 2
26+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = args1[0].kind
27+
&& let ExprKind::Array(elements) = inner.kind
28+
&& elements.len() == 2
29+
&& let ExprKind::Lit(ref lit2) = elements[0].kind
30+
&& let LitKind::Str(s, _) = lit2.node
31+
&& s.as_str() == ""
32+
&& let ExprKind::Lit(ref lit3) = elements[1].kind
33+
&& let LitKind::Str(s1, _) = lit3.node
34+
&& s1.as_str() == "\n"
35+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner1) = args1[1].kind
36+
&& let ExprKind::Array(elements1) = inner1.kind
37+
&& elements1.len() == 1
38+
&& let ExprKind::Call(func2, args2) = elements1[0].kind
39+
&& let ExprKind::Path(ref qpath3) = func2.kind
40+
&& args2.len() == 1
41+
&& let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner2) = args2[0].kind
42+
&& let ExprKind::Path(ref qpath4) = inner2.kind
43+
&& match_qpath(qpath4, &["i"])
44+
&& block1.expr.is_none()
45+
&& block.expr.is_none()
46+
{
47+
// report your lint here
48+
}

0 commit comments

Comments
 (0)