Skip to content

Commit eb6fb18

Browse files
committed
Avoid panic!, omit instead
1 parent 929a288 commit eb6fb18

File tree

3 files changed

+54
-8
lines changed

3 files changed

+54
-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+
}

0 commit comments

Comments
 (0)