Skip to content

Commit

Permalink
Merge pull request #344 from CoinFabrik/avoid-panic-error-fix
Browse files Browse the repository at this point in the history
Fix panic on invalid `unwrap`
  • Loading branch information
matiascabello authored Aug 29, 2024
2 parents 9673846 + 521a326 commit 9d8f07d
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions detectors/avoid-panic-error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate rustc_span;

use clippy_utils::diagnostics::span_lint_and_help;
use rustc_ast::{
ptr::P,
tokenstream::TokenTree,
visit::{walk_expr, Visitor},
AssocItemKind, AttrArgs, AttrKind, Block, Expr, ExprKind, FnRetTy, Item, ItemKind, MacCall,
Expand Down Expand Up @@ -93,16 +94,12 @@ impl EarlyLintPass for AvoidPanicError {
ItemKind::Impl(impl_item) => {
for assoc_item in &impl_item.items {
if let AssocItemKind::Fn(fn_item) = &assoc_item.kind {
self.check_function(
cx,
&fn_item.sig.decl.output,
fn_item.body.as_ref().unwrap(),
);
self.check_function(cx, &fn_item.sig.decl.output, &fn_item.body);
}
}
}
ItemKind::Fn(fn_item) => {
self.check_function(cx, &fn_item.sig.decl.output, fn_item.body.as_ref().unwrap());
self.check_function(cx, &fn_item.sig.decl.output, &fn_item.body);
}
ItemKind::Mod(_, ModKind::Loaded(items, _, _)) => {
for item in items {
Expand All @@ -112,11 +109,7 @@ impl EarlyLintPass for AvoidPanicError {
ItemKind::Trait(trait_item) => {
for item in &trait_item.items {
if let AssocItemKind::Fn(fn_item) = &item.kind {
self.check_function(
cx,
&fn_item.sig.decl.output,
fn_item.body.as_ref().unwrap(),
);
self.check_function(cx, &fn_item.sig.decl.output, &fn_item.body);
}
}
}
Expand All @@ -126,10 +119,12 @@ impl EarlyLintPass for AvoidPanicError {
}

impl AvoidPanicError {
fn check_function(&self, cx: &EarlyContext, output: &FnRetTy, body: &Block) {
if is_result_type(output) {
let mut visitor = PanicVisitor { cx };
visitor.visit_block(body);
fn check_function(&self, cx: &EarlyContext, output: &FnRetTy, body: &Option<P<Block>>) {
if let Some(body) = body {
if is_result_type(output) {
let mut visitor = PanicVisitor { cx };
visitor.visit_block(body);
}
}
}
}
Expand Down

0 comments on commit 9d8f07d

Please sign in to comment.