Skip to content

Commit e8185ec

Browse files
authored
Extend implicit_clone to handle to_string calls (#14177)
Put another way, merge `string_to_string` into `implicit_clone`, as suggested here: #14173 (comment) Note: [I wrote](b891389) this comment: https://github.com/rust-lang/rust-clippy/blob/6cdb7f68c39a2458c6b8f6dc63da4123a6a5af89/clippy_lints/src/methods/implicit_clone.rs#L43-L45 Here is the context for why I wrote it: #7978 (comment) Regardless, it's probably time for the comment to go away. Extending `implicit_clone` to handle `to_string` calls yields many hits within Clippy's codebase. changelog: extend `implicit_clone` to handle `to_string` calls
2 parents ffcd129 + e910c0e commit e8185ec

28 files changed

+61
-285
lines changed

.github/driver.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ unset CARGO_MANIFEST_DIR
4747

4848
# Run a lint and make sure it produces the expected output. It's also expected to exit with code 1
4949
# FIXME: How to match the clippy invocation in compile-test.rs?
50-
./target/debug/clippy-driver -Dwarnings -Aunused -Zui-testing --emit metadata --crate-type bin tests/ui/string_to_string.rs 2>string_to_string.stderr && exit 1
51-
sed -e "/= help: for/d" string_to_string.stderr > normalized.stderr
52-
diff -u normalized.stderr tests/ui/string_to_string.stderr
50+
./target/debug/clippy-driver -Dwarnings -Aunused -Zui-testing --emit metadata --crate-type bin tests/ui/char_lit_as_u8.rs 2>char_lit_as_u8.stderr && exit 1
51+
sed -e "/= help: for/d" char_lit_as_u8.stderr > normalized.stderr
52+
diff -u normalized.stderr tests/ui/char_lit_as_u8.stderr
5353

5454
# make sure "clippy-driver --rustc --arg" and "rustc --arg" behave the same
5555
SYSROOT=$(rustc --print sysroot)

clippy_lints/src/blocks_in_conditions.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
100100
cond.span,
101101
BRACED_EXPR_MESSAGE,
102102
"try",
103-
snippet_block_with_applicability(cx, ex.span, "..", Some(expr.span), &mut applicability)
104-
.to_string(),
103+
snippet_block_with_applicability(cx, ex.span, "..", Some(expr.span), &mut applicability),
105104
applicability,
106105
);
107106
}

clippy_lints/src/copies.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,9 @@ fn lint_branches_sharing_code<'tcx>(
235235
let cond_snippet = reindent_multiline(&snippet(cx, cond_span, "_"), false, None);
236236
let cond_indent = indent_of(cx, cond_span);
237237
let moved_snippet = reindent_multiline(&snippet(cx, span, "_"), true, None);
238-
let suggestion = moved_snippet.to_string() + "\n" + &cond_snippet + "{";
238+
let suggestion = moved_snippet + "\n" + &cond_snippet + "{";
239239
let suggestion = reindent_multiline(&suggestion, true, cond_indent);
240-
(replace_span, suggestion.to_string())
240+
(replace_span, suggestion)
241241
});
242242
let end_suggestion = res.end_span(last_block, sm).map(|span| {
243243
let moved_snipped = reindent_multiline(&snippet(cx, span, "_"), true, None);
@@ -253,7 +253,7 @@ fn lint_branches_sharing_code<'tcx>(
253253
.then_some(range.start - 4..range.end)
254254
})
255255
.map_or(span, |range| range.with_ctxt(span.ctxt()));
256-
(span, suggestion.to_string())
256+
(span, suggestion.clone())
257257
});
258258

259259
let (span, msg, end_span) = match (&start_suggestion, &end_suggestion) {

clippy_lints/src/declared_lints.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
690690
crate::strings::STRING_FROM_UTF8_AS_BYTES_INFO,
691691
crate::strings::STRING_LIT_AS_BYTES_INFO,
692692
crate::strings::STRING_SLICE_INFO,
693-
crate::strings::STRING_TO_STRING_INFO,
694693
crate::strings::STR_TO_STRING_INFO,
695694
crate::strings::TRIM_SPLIT_WHITESPACE_INFO,
696695
crate::strlen_on_c_strings::STRLEN_ON_C_STRINGS_INFO,

clippy_lints/src/deprecated_lints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ declare_with_version! { DEPRECATED(DEPRECATED_VERSION) = [
3434
("clippy::replace_consts", "`min_value` and `max_value` are now deprecated"),
3535
#[clippy::version = "pre 1.29.0"]
3636
("clippy::should_assert_eq", "`assert!(a == b)` can now print the values the same way `assert_eq!(a, b) can"),
37+
#[clippy::version = "1.90.0"]
38+
("clippy::string_to_string", "`clippy:implicit_clone` covers those cases"),
3739
#[clippy::version = "pre 1.29.0"]
3840
("clippy::unsafe_vector_initialization", "the suggested alternative could be substantially slower"),
3941
#[clippy::version = "pre 1.29.0"]

clippy_lints/src/if_not_else.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl LateLintPass<'_> for IfNotElse {
8181
e.span,
8282
msg,
8383
"try",
84-
make_sugg(cx, &cond.kind, cond_inner.span, els.span, "..", Some(e.span)).to_string(),
84+
make_sugg(cx, &cond.kind, cond_inner.span, els.span, "..", Some(e.span)),
8585
Applicability::MachineApplicable,
8686
),
8787
_ => span_lint_and_help(cx, IF_NOT_ELSE, e.span, msg, None, help),

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,6 @@ pub fn register_lint_passes(store: &mut rustc_lint::LintStore, conf: &'static Co
665665
store.register_early_pass(|| Box::new(asm_syntax::InlineAsmX86IntelSyntax));
666666
store.register_late_pass(|_| Box::new(empty_drop::EmptyDrop));
667667
store.register_late_pass(|_| Box::new(strings::StrToString));
668-
store.register_late_pass(|_| Box::new(strings::StringToString));
669668
store.register_late_pass(|_| Box::new(zero_sized_map_values::ZeroSizedMapValues));
670669
store.register_late_pass(|_| Box::<vec_init_then_push::VecInitThenPush>::default());
671670
store.register_late_pass(|_| Box::new(redundant_slicing::RedundantSlicing));

clippy_lints/src/manual_async_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualAsyncFn {
8383
format!("{} async {}", vis_snip, &header_snip[vis_snip.len() + 1..ret_pos])
8484
};
8585

86-
let body_snip = snippet_block(cx, closure_body.value.span, "..", Some(block.span)).to_string();
86+
let body_snip = snippet_block(cx, closure_body.value.span, "..", Some(block.span));
8787

8888
diag.multipart_suggestion(
8989
"make the function `async` and return the output of the future directly",

clippy_lints/src/matches/match_single_binding.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ pub(crate) fn check<'a>(cx: &LateContext<'a>, ex: &Expr<'a>, arms: &[Arm<'_>], e
3131
let match_body = peel_blocks(arms[0].body);
3232
let mut app = Applicability::MaybeIncorrect;
3333
let ctxt = expr.span.ctxt();
34-
let mut snippet_body = snippet_block_with_context(cx, match_body.span, ctxt, "..", Some(expr.span), &mut app)
35-
.0
36-
.to_string();
34+
let mut snippet_body = snippet_block_with_context(cx, match_body.span, ctxt, "..", Some(expr.span), &mut app).0;
3735

3836
// Do we need to add ';' to suggestion ?
3937
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)

clippy_lints/src/matches/single_match.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ fn report_single_pattern(
112112
let (sugg, help) = if is_unit_expr(arm.body) {
113113
(String::new(), "`match` expression can be removed")
114114
} else {
115-
let mut sugg = snippet_block_with_context(cx, arm.body.span, ctxt, "..", Some(expr.span), &mut app)
116-
.0
117-
.to_string();
115+
let mut sugg = snippet_block_with_context(cx, arm.body.span, ctxt, "..", Some(expr.span), &mut app).0;
118116
if let Node::Stmt(stmt) = cx.tcx.parent_hir_node(expr.hir_id)
119117
&& let StmtKind::Expr(_) = stmt.kind
120118
&& match arm.body.kind {
@@ -127,7 +125,7 @@ fn report_single_pattern(
127125
(sugg, "try")
128126
};
129127
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
130-
diag.span_suggestion(expr.span, help, sugg.to_string(), app);
128+
diag.span_suggestion(expr.span, help, sugg, app);
131129
note(diag);
132130
});
133131
return;
@@ -188,7 +186,7 @@ fn report_single_pattern(
188186
};
189187

190188
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
191-
diag.span_suggestion(expr.span, "try", sugg.to_string(), app);
189+
diag.span_suggestion(expr.span, "try", sugg, app);
192190
note(diag);
193191
});
194192
}

0 commit comments

Comments
 (0)