Skip to content

Commit 2785063

Browse files
committed
Avoid kw::Empty use for AuxParamsAttr.
By changing two of the fields to use `Option<Ident>` instead of `Ident`. As a result, `None` now means "no identifier", which is much clearer than using an empty identifier.
1 parent 929749d commit 2785063

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Diff for: src/tools/clippy/clippy_lints/src/significant_drop_tightening.rs

+16-14
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
7979
if apa.counter <= 1 || !apa.has_expensive_expr_after_last_attr {
8080
continue;
8181
}
82+
let first_bind_ident = apa.first_bind_ident.unwrap();
8283
span_lint_and_then(
8384
cx,
8485
SIGNIFICANT_DROP_TIGHTENING,
85-
apa.first_bind_ident.span,
86+
first_bind_ident.span,
8687
"temporary with significant `Drop` can be early dropped",
8788
|diag| {
8889
match apa.counter {
@@ -91,13 +92,13 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
9192
let indent = " ".repeat(indent_of(cx, apa.last_stmt_span).unwrap_or(0));
9293
let init_method = snippet(cx, apa.first_method_span, "..");
9394
let usage_method = snippet(cx, apa.last_method_span, "..");
94-
let stmt = if apa.last_bind_ident == Ident::empty() {
95-
format!("\n{indent}{init_method}.{usage_method};")
96-
} else {
95+
let stmt = if let Some(last_bind_ident) = apa.last_bind_ident {
9796
format!(
9897
"\n{indent}let {} = {init_method}.{usage_method};",
99-
snippet(cx, apa.last_bind_ident.span, ".."),
98+
snippet(cx, last_bind_ident.span, ".."),
10099
)
100+
} else {
101+
format!("\n{indent}{init_method}.{usage_method};")
101102
};
102103

103104
diag.multipart_suggestion_verbose(
@@ -113,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
113114
format!(
114115
"\n{}drop({});",
115116
" ".repeat(indent_of(cx, apa.last_stmt_span).unwrap_or(0)),
116-
apa.first_bind_ident
117+
first_bind_ident
117118
),
118119
Applicability::MaybeIncorrect,
119120
);
@@ -124,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for SignificantDropTightening<'tcx> {
124125
apa.first_block_span,
125126
format!(
126127
"temporary `{}` is currently being dropped at the end of its contained scope",
127-
apa.first_bind_ident
128+
first_bind_ident
128129
),
129130
);
130131
},
@@ -283,7 +284,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
283284
let mut apa = AuxParamsAttr {
284285
first_block_hir_id: self.ap.curr_block_hir_id,
285286
first_block_span: self.ap.curr_block_span,
286-
first_bind_ident: ident,
287+
first_bind_ident: Some(ident),
287288
first_method_span: {
288289
let expr_or_init = expr_or_init(self.cx, expr);
289290
if let hir::ExprKind::MethodCall(_, local_expr, _, span) = expr_or_init.kind {
@@ -307,7 +308,7 @@ impl<'tcx> Visitor<'tcx> for StmtsChecker<'_, '_, '_, '_, 'tcx> {
307308
match self.ap.curr_stmt.kind {
308309
hir::StmtKind::Let(local) => {
309310
if let hir::PatKind::Binding(_, _, ident, _) = local.pat.kind {
310-
apa.last_bind_ident = ident;
311+
apa.last_bind_ident = Some(ident);
311312
}
312313
if let Some(local_init) = local.init
313314
&& let hir::ExprKind::MethodCall(_, _, _, span) = local_init.kind
@@ -373,15 +374,15 @@ struct AuxParamsAttr {
373374
first_block_span: Span,
374375
/// The binding or variable that references the initial construction of the type marked with
375376
/// `#[has_significant_drop]`.
376-
first_bind_ident: Ident,
377+
first_bind_ident: Option<Ident>,
377378
/// Similar to `init_bind_ident` but encompasses the right-hand method call.
378379
first_method_span: Span,
379380
/// Similar to `init_bind_ident` but encompasses the whole contained statement.
380381
first_stmt_span: Span,
381382

382383
/// The last visited binding or variable span within a block that had any referenced inner type
383384
/// marked with `#[has_significant_drop]`.
384-
last_bind_ident: Ident,
385+
last_bind_ident: Option<Ident>,
385386
/// Similar to `last_bind_span` but encompasses the right-hand method call.
386387
last_method_span: Span,
387388
/// Similar to `last_bind_span` but encompasses the whole contained statement.
@@ -395,10 +396,10 @@ impl Default for AuxParamsAttr {
395396
has_expensive_expr_after_last_attr: false,
396397
first_block_hir_id: HirId::INVALID,
397398
first_block_span: DUMMY_SP,
398-
first_bind_ident: Ident::empty(),
399+
first_bind_ident: None,
399400
first_method_span: DUMMY_SP,
400401
first_stmt_span: DUMMY_SP,
401-
last_bind_ident: Ident::empty(),
402+
last_bind_ident: None,
402403
last_method_span: DUMMY_SP,
403404
last_stmt_span: DUMMY_SP,
404405
}
@@ -413,7 +414,7 @@ fn dummy_stmt_expr<'any>(expr: &'any hir::Expr<'any>) -> hir::Stmt<'any> {
413414
}
414415
}
415416

416-
fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_>) -> bool {
417+
fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Option<Ident>, lcx: &LateContext<'_>) -> bool {
417418
if let hir::ExprKind::Call(fun, [first_arg]) = expr.kind
418419
&& let hir::ExprKind::Path(hir::QPath::Resolved(_, fun_path)) = &fun.kind
419420
&& let Res::Def(DefKind::Fn, did) = fun_path.res
@@ -422,6 +423,7 @@ fn has_drop(expr: &hir::Expr<'_>, first_bind_ident: &Ident, lcx: &LateContext<'_
422423
let has_ident = |local_expr: &hir::Expr<'_>| {
423424
if let hir::ExprKind::Path(hir::QPath::Resolved(_, arg_path)) = &local_expr.kind
424425
&& let [first_arg_ps, ..] = arg_path.segments
426+
&& let Some(first_bind_ident) = first_bind_ident
425427
&& &first_arg_ps.ident == first_bind_ident
426428
{
427429
true

0 commit comments

Comments
 (0)