Skip to content

Commit

Permalink
refactored the code
Browse files Browse the repository at this point in the history
  • Loading branch information
Samarth1696 committed Sep 7, 2024
1 parent 0f99aa9 commit bed4441
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions clippy_lints/src/non_zero_suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,22 @@ declare_lint_pass!(NonZeroSuggestions => [NON_ZERO_SUGGESTIONS]);

impl<'tcx> LateLintPass<'tcx> for NonZeroSuggestions {
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
if let ExprKind::Call(func, [arg]) = expr.kind {
if let ExprKind::Path(qpath) = &func.kind {
if let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id() {
let fn_name = cx.tcx.item_name(def_id);
let target_ty = cx.typeck_results().expr_ty(expr);
if let ExprKind::Call(func, [arg]) = expr.kind
&& let ExprKind::Path(qpath) = &func.kind
&& let Some(def_id) = cx.qpath_res(qpath, func.hir_id).opt_def_id()
&& let ExprKind::MethodCall(rcv_path, receiver, _, _) = &arg.kind
{
let fn_name = cx.tcx.item_name(def_id);
let target_ty = cx.typeck_results().expr_ty(expr);
let receiver_ty = cx.typeck_results().expr_ty(receiver);

if let ExprKind::MethodCall(rcv_path, receiver, _, _) = &arg.kind {
let receiver_ty = cx.typeck_results().expr_ty(receiver);
if let ty::Adt(adt_def, _) = receiver_ty.kind() {
if adt_def.is_struct() && cx.tcx.get_diagnostic_name(adt_def.did()) == Some(sym::NonZero) {
if let Some(target_non_zero_type) = get_target_non_zero_type(target_ty) {
let arg_snippet = get_arg_snippet(cx, arg, rcv_path);
suggest_non_zero_conversion(cx, expr, fn_name, target_non_zero_type, &arg_snippet);
}
}
}
}
if let ty::Adt(adt_def, _) = receiver_ty.kind()
&& adt_def.is_struct()
&& cx.tcx.get_diagnostic_name(adt_def.did()) == Some(sym::NonZero)
{
if let Some(target_non_zero_type) = get_target_non_zero_type(target_ty) {
let arg_snippet = get_arg_snippet(cx, arg, rcv_path);
suggest_non_zero_conversion(cx, expr, fn_name, target_non_zero_type, &arg_snippet);
}
}
}
Expand Down

0 comments on commit bed4441

Please sign in to comment.