Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions clippy_lints/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use clippy_utils::{
SpanlessEq, can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified,
peel_hir_expr_while,
};
use core::fmt::{self, Write};
use core::fmt::Write;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, why not use std::fmt::Write since std::fmt is also imported?

use rustc_errors::Applicability;
use rustc_hir::hir_id::HirIdSet;
use rustc_hir::intravisit::{Visitor, walk_body, walk_expr};
use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, Stmt, StmtKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::declare_lint_pass;
use rustc_span::{DUMMY_SP, Span, SyntaxContext, sym};
use std::fmt;
use std::ops::ControlFlow;

declare_clippy_lint! {
Expand Down Expand Up @@ -628,11 +629,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
if is_expr_used_or_unified(cx.tcx, insertion.call) {
write_wrapped(&mut res, insertion, ctxt, app);
} else {
let _: fmt::Result = write!(
res,
"e.insert({})",
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0
);
let value_str = snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're cleaning up, "_" would be a better choice than ".." for a single value (even though this is never used…). Same below.

let _: fmt::Result = write!(res, "e.insert({value_str})");
}
span = span.trim_start(insertion.call.span).unwrap_or(DUMMY_SP);
}
Expand All @@ -644,11 +642,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
(
self.snippet(cx, span, app, |res, insertion, ctxt, app| {
// Insertion into a map would return `Some(&mut value)`, but the entry returns `&mut value`
let _: fmt::Result = write!(
res,
"Some(e.insert({}))",
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0
);
let value_str = snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0;
let _: fmt::Result = write!(res, "Some(e.insert({value_str}))");
}),
"Occupied(mut e)",
)
Expand All @@ -658,19 +653,15 @@ impl<'tcx> InsertSearchResults<'tcx> {
(
self.snippet(cx, span, app, |res, insertion, ctxt, app| {
// Insertion into a map would return `None`, but the entry returns a mutable reference.
let value_str = snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0;
let _: fmt::Result = if is_expr_final_block_expr(cx.tcx, insertion.call) {
write!(
res,
"e.insert({});\n{}None",
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0,
snippet_indent(cx, insertion.call.span).as_deref().unwrap_or(""),
"e.insert({value_str});\n{indent}None",
indent = snippet_indent(cx, insertion.call.span).as_deref().unwrap_or(""),
)
} else {
write!(
res,
"{{ e.insert({}); None }}",
snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0,
)
write!(res, "{{ e.insert({value_str}); None }}")
};
}),
"Vacant(e)",
Expand All @@ -690,7 +681,8 @@ impl<'tcx> InsertSearchResults<'tcx> {
"..",
app,
));
res.push_str(&snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0);
let value_str = snippet_with_context(cx, insertion.value.span, ctxt, "..", app).0;
let _: fmt::Result = write!(res, "{value_str}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not res.push_str(&value_str); which is infallible?

span = span.trim_start(insertion.call.span).unwrap_or(DUMMY_SP);
},
Edit::RemoveSemi(semi_span) => {
Expand Down