Skip to content

Commit 96e70a6

Browse files
perf: optimize sugg
1 parent 25f3a7e commit 96e70a6

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

clippy_lints/src/nonnull_unchecked_on_box_ptr.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::res::{MaybeDef, MaybeQPath};
55
use clippy_utils::source::snippet_with_context;
@@ -75,22 +75,23 @@ impl<'tcx> LateLintPass<'tcx> for NonnullUncheckedOnBoxPtr {
7575
_ => expr.span,
7676
};
7777

78-
let mut app = Applicability::MachineApplicable;
79-
let arg_name = snippet_with_context(cx, arg.span, ctxt, "_", &mut app).0;
80-
let sugg = if self.msrv.meets(cx, msrvs::NONNULL_FROM_MUT) {
81-
format!("NonNull::from_mut(Box::leak({arg_name}))")
82-
} else {
83-
format!("NonNull::from(Box::leak({arg_name}))")
84-
};
85-
86-
span_lint_and_sugg(
78+
span_lint_and_then(
8779
cx,
8880
NONNULL_UNCHECKED_ON_BOX_PTR,
8981
span,
9082
"use of `NonNull::new_unchecked` with `Box::into_raw`",
91-
"try",
92-
sugg,
93-
app,
83+
|diag| {
84+
let mut app = Applicability::MachineApplicable;
85+
let arg_name = snippet_with_context(cx, arg.span, ctxt, "_", &mut app).0;
86+
87+
let sugg = if self.msrv.meets(cx, msrvs::NONNULL_FROM_MUT) {
88+
format!("NonNull::from_mut(Box::leak({arg_name}))")
89+
} else {
90+
format!("NonNull::from(Box::leak({arg_name}))")
91+
};
92+
93+
diag.span_suggestion(span, "try", sugg, app);
94+
},
9495
);
9596
}
9697
}

0 commit comments

Comments
 (0)