Skip to content

Commit 7e92655

Browse files
committed
Auto merge of #16521 - tetsuharuohzeki:experiment-enable-str_to_string, r=lnicola
internal: Enable str_to_string Clippy rule This fix [the FIXME comment](https://github.com/rust-lang/rust-analyzer/blob/bb0de88f24cc3a8de10837585fbb8172f6859752/Cargo.toml#L183-L184)
2 parents bb0de88 + 71ea70e commit 7e92655

File tree

149 files changed

+382
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+382
-391
lines changed

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -180,5 +180,4 @@ print_stdout = "warn"
180180
print_stderr = "warn"
181181

182182
rc_buffer = "warn"
183-
# FIXME enable this, we use this pattern a lot so its annoying work ...
184-
# str_to_string = "warn"
183+
str_to_string = "warn"

crates/base-db/src/input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ impl FromStr for Edition {
782782
"2018" => Edition::Edition2018,
783783
"2021" => Edition::Edition2021,
784784
"2024" => Edition::Edition2024,
785-
_ => return Err(ParseEditionError { invalid_input: s.to_string() }),
785+
_ => return Err(ParseEditionError { invalid_input: s.to_owned() }),
786786
};
787787
Ok(res)
788788
}

crates/hir-def/src/body/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo
2929
"const {} = ",
3030
match &it.name {
3131
Some(name) => name.display(db.upcast()).to_string(),
32-
None => "_".to_string(),
32+
None => "_".to_owned(),
3333
}
3434
)
3535
}),
36-
DefWithBodyId::InTypeConstId(_) => "In type const = ".to_string(),
36+
DefWithBodyId::InTypeConstId(_) => "In type const = ".to_owned(),
3737
DefWithBodyId::VariantId(it) => {
3838
let loc = it.lookup(db);
3939
let enum_loc = loc.parent.lookup(db);
@@ -123,7 +123,7 @@ impl Printer<'_> {
123123
wln!(self);
124124
f(self);
125125
self.indent_level -= 1;
126-
self.buf = self.buf.trim_end_matches('\n').to_string();
126+
self.buf = self.buf.trim_end_matches('\n').to_owned();
127127
}
128128

129129
fn whitespace(&mut self) {

crates/hir-def/src/import_map.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ mod tests {
859859
check_search(
860860
ra_fixture,
861861
"main",
862-
Query::new("fmt".to_string()).fuzzy(),
862+
Query::new("fmt".to_owned()).fuzzy(),
863863
expect![[r#"
864864
dep::fmt (t)
865865
dep::fmt::Display::FMT_CONST (a)
@@ -888,9 +888,7 @@ mod tests {
888888
check_search(
889889
ra_fixture,
890890
"main",
891-
Query::new("fmt".to_string())
892-
.fuzzy()
893-
.assoc_search_mode(AssocSearchMode::AssocItemsOnly),
891+
Query::new("fmt".to_owned()).fuzzy().assoc_search_mode(AssocSearchMode::AssocItemsOnly),
894892
expect![[r#"
895893
dep::fmt::Display::FMT_CONST (a)
896894
dep::fmt::Display::format_function (a)
@@ -901,7 +899,7 @@ mod tests {
901899
check_search(
902900
ra_fixture,
903901
"main",
904-
Query::new("fmt".to_string()).fuzzy().assoc_search_mode(AssocSearchMode::Exclude),
902+
Query::new("fmt".to_owned()).fuzzy().assoc_search_mode(AssocSearchMode::Exclude),
905903
expect![[r#"
906904
dep::fmt (t)
907905
"#]],
@@ -937,7 +935,7 @@ pub mod fmt {
937935
check_search(
938936
ra_fixture,
939937
"main",
940-
Query::new("fmt".to_string()).fuzzy(),
938+
Query::new("fmt".to_owned()).fuzzy(),
941939
expect![[r#"
942940
dep::Fmt (m)
943941
dep::Fmt (t)
@@ -951,7 +949,7 @@ pub mod fmt {
951949
check_search(
952950
ra_fixture,
953951
"main",
954-
Query::new("fmt".to_string()),
952+
Query::new("fmt".to_owned()),
955953
expect![[r#"
956954
dep::Fmt (m)
957955
dep::Fmt (t)
@@ -991,7 +989,7 @@ pub mod fmt {
991989
check_search(
992990
ra_fixture,
993991
"main",
994-
Query::new("fmt".to_string()),
992+
Query::new("fmt".to_owned()),
995993
expect![[r#"
996994
dep::Fmt (m)
997995
dep::Fmt (t)
@@ -1015,7 +1013,7 @@ pub mod fmt {
10151013
check_search(
10161014
ra_fixture,
10171015
"main",
1018-
Query::new("FMT".to_string()),
1016+
Query::new("FMT".to_owned()),
10191017
expect![[r#"
10201018
dep::FMT (t)
10211019
dep::FMT (v)
@@ -1027,7 +1025,7 @@ pub mod fmt {
10271025
check_search(
10281026
ra_fixture,
10291027
"main",
1030-
Query::new("FMT".to_string()).case_sensitive(),
1028+
Query::new("FMT".to_owned()).case_sensitive(),
10311029
expect![[r#"
10321030
dep::FMT (t)
10331031
dep::FMT (v)

crates/hir-def/src/item_scope.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ impl ItemScope {
672672
format_to!(
673673
buf,
674674
"{}:",
675-
name.map_or("_".to_string(), |name| name.display(db).to_string())
675+
name.map_or("_".to_owned(), |name| name.display(db).to_string())
676676
);
677677

678678
if let Some((.., i)) = def.types {

crates/hir-def/src/item_tree/pretty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(super) fn print_item_tree(db: &dyn DefDatabase, tree: &ItemTree) -> String {
2424
p.print_mod_item(*item);
2525
}
2626

27-
let mut s = p.buf.trim_end_matches('\n').to_string();
27+
let mut s = p.buf.trim_end_matches('\n').to_owned();
2828
s.push('\n');
2929
s
3030
}
@@ -58,7 +58,7 @@ impl Printer<'_> {
5858
wln!(self);
5959
f(self);
6060
self.indent_level -= 1;
61-
self.buf = self.buf.trim_end_matches('\n').to_string();
61+
self.buf = self.buf.trim_end_matches('\n').to_owned();
6262
}
6363

6464
/// Ensures that a blank line is output before the next text.

crates/hir-def/src/macro_expansion_tests/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn reindent(indent: IndentLevel, pp: String) -> String {
224224
return pp;
225225
}
226226
let mut lines = pp.split_inclusive('\n');
227-
let mut res = lines.next().unwrap().to_string();
227+
let mut res = lines.next().unwrap().to_owned();
228228
for line in lines {
229229
if line.trim().is_empty() {
230230
res.push_str(line)

crates/hir-expand/src/builtin_fn_macro.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ fn concat_bytes_expand(
515515
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
516516
let token = ast::make::tokens::literal(&lit.to_string());
517517
match token.kind() {
518-
syntax::SyntaxKind::BYTE => bytes.push(token.text().to_string()),
518+
syntax::SyntaxKind::BYTE => bytes.push(token.text().to_owned()),
519519
syntax::SyntaxKind::BYTE_STRING => {
520520
let components = unquote_byte_string(lit).unwrap_or_default();
521521
components.into_iter().for_each(|it| bytes.push(it.to_string()));
@@ -570,7 +570,7 @@ fn concat_bytes_expand_subtree(
570570
let lit = ast::make::tokens::literal(&lit.to_string());
571571
match lit.kind() {
572572
syntax::SyntaxKind::BYTE | syntax::SyntaxKind::INT_NUMBER => {
573-
bytes.push(lit.text().to_string())
573+
bytes.push(lit.text().to_owned())
574574
}
575575
_ => {
576576
return Err(mbe::ExpandError::UnexpectedToken.into());
@@ -749,7 +749,7 @@ fn env_expand(
749749
// We cannot use an empty string here, because for
750750
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
751751
// `include!("foo.rs"), which might go to infinite loop
752-
"UNRESOLVED_ENV_VAR".to_string()
752+
"UNRESOLVED_ENV_VAR".to_owned()
753753
});
754754
let expanded = quote! {span => #s };
755755

crates/hir-ty/src/consteval/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ fn bit_op() {
133133
check_number(r#"const GOAL: i8 = 1 << 7"#, (1i8 << 7) as i128);
134134
check_number(r#"const GOAL: i8 = -1 << 2"#, (-1i8 << 2) as i128);
135135
check_fail(r#"const GOAL: i8 = 1 << 8"#, |e| {
136-
e == ConstEvalError::MirEvalError(MirEvalError::Panic("Overflow in Shl".to_string()))
136+
e == ConstEvalError::MirEvalError(MirEvalError::Panic("Overflow in Shl".to_owned()))
137137
});
138138
check_number(r#"const GOAL: i32 = 100000000i32 << 11"#, (100000000i32 << 11) as i128);
139139
}
@@ -2756,7 +2756,7 @@ fn memory_limit() {
27562756
"#,
27572757
|e| {
27582758
e == ConstEvalError::MirEvalError(MirEvalError::Panic(
2759-
"Memory allocation of 30000000000 bytes failed".to_string(),
2759+
"Memory allocation of 30000000000 bytes failed".to_owned(),
27602760
))
27612761
},
27622762
);

crates/hir-ty/src/infer/closure.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,15 @@ impl CapturedItem {
194194
}
195195
let variant_data = f.parent.variant_data(db.upcast());
196196
let field = match &*variant_data {
197-
VariantData::Record(fields) => fields[f.local_id]
198-
.name
199-
.as_str()
200-
.unwrap_or("[missing field]")
201-
.to_string(),
197+
VariantData::Record(fields) => {
198+
fields[f.local_id].name.as_str().unwrap_or("[missing field]").to_owned()
199+
}
202200
VariantData::Tuple(fields) => fields
203201
.iter()
204202
.position(|it| it.0 == f.local_id)
205203
.unwrap_or_default()
206204
.to_string(),
207-
VariantData::Unit => "[missing field]".to_string(),
205+
VariantData::Unit => "[missing field]".to_owned(),
208206
};
209207
result = format!("{result}.{field}");
210208
field_need_paren = false;

crates/hir-ty/src/mir/eval.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1763,7 +1763,7 @@ impl Evaluator<'_> {
17631763
}
17641764
};
17651765
mem.get(pos..pos + size)
1766-
.ok_or_else(|| MirEvalError::UndefinedBehavior("out of bound memory read".to_string()))
1766+
.ok_or_else(|| MirEvalError::UndefinedBehavior("out of bound memory read".to_owned()))
17671767
}
17681768

17691769
fn write_memory_using_ref(&mut self, addr: Address, size: usize) -> Result<&mut [u8]> {
@@ -1777,7 +1777,7 @@ impl Evaluator<'_> {
17771777
}
17781778
};
17791779
mem.get_mut(pos..pos + size)
1780-
.ok_or_else(|| MirEvalError::UndefinedBehavior("out of bound memory write".to_string()))
1780+
.ok_or_else(|| MirEvalError::UndefinedBehavior("out of bound memory write".to_owned()))
17811781
}
17821782

17831783
fn write_memory(&mut self, addr: Address, r: &[u8]) -> Result<()> {
@@ -1800,7 +1800,7 @@ impl Evaluator<'_> {
18001800
return Ok(());
18011801
}
18021802

1803-
let oob = || MirEvalError::UndefinedBehavior("out of bounds memory write".to_string());
1803+
let oob = || MirEvalError::UndefinedBehavior("out of bounds memory write".to_owned());
18041804

18051805
match (addr, r.addr) {
18061806
(Stack(dst), Stack(src)) => {
@@ -2653,7 +2653,7 @@ pub fn render_const_using_debug_impl(
26532653
ptr: ArenaMap::new(),
26542654
body: db
26552655
.mir_body(owner.into())
2656-
.map_err(|_| MirEvalError::NotSupported("unreachable".to_string()))?,
2656+
.map_err(|_| MirEvalError::NotSupported("unreachable".to_owned()))?,
26572657
drop_flags: DropFlags::default(),
26582658
};
26592659
let data = evaluator.allocate_const_in_heap(locals, c)?;

crates/hir-ty/src/mir/eval/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ impl Evaluator<'_> {
304304
use LangItem::*;
305305
let mut args = args.iter();
306306
match it {
307-
BeginPanic => Err(MirEvalError::Panic("<unknown-panic-payload>".to_string())),
307+
BeginPanic => Err(MirEvalError::Panic("<unknown-panic-payload>".to_owned())),
308308
PanicFmt => {
309309
let message = (|| {
310310
let resolver = self

crates/hir-ty/src/mir/lower.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
16341634
self.set_goto(prev_block, begin, span);
16351635
f(self, begin)?;
16361636
let my = mem::replace(&mut self.current_loop_blocks, prev).ok_or(
1637-
MirLowerError::ImplementationError("current_loop_blocks is corrupt".to_string()),
1637+
MirLowerError::ImplementationError("current_loop_blocks is corrupt".to_owned()),
16381638
)?;
16391639
if let Some(prev) = prev_label {
16401640
self.labeled_loop_blocks.insert(label.unwrap(), prev);
@@ -1669,7 +1669,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
16691669
.current_loop_blocks
16701670
.as_mut()
16711671
.ok_or(MirLowerError::ImplementationError(
1672-
"Current loop access out of loop".to_string(),
1672+
"Current loop access out of loop".to_owned(),
16731673
))?
16741674
.end
16751675
{
@@ -1679,7 +1679,7 @@ impl<'ctx> MirLowerCtx<'ctx> {
16791679
self.current_loop_blocks
16801680
.as_mut()
16811681
.ok_or(MirLowerError::ImplementationError(
1682-
"Current loop access out of loop".to_string(),
1682+
"Current loop access out of loop".to_owned(),
16831683
))?
16841684
.end = Some(s);
16851685
s

crates/hir-ty/src/mir/lower/as_place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl MirLowerCtx<'_> {
225225
{
226226
let Some(index_fn) = self.infer.method_resolution(expr_id) else {
227227
return Err(MirLowerError::UnresolvedMethod(
228-
"[overloaded index]".to_string(),
228+
"[overloaded index]".to_owned(),
229229
));
230230
};
231231
let Some((base_place, current)) =

crates/hir-ty/src/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
100100
if only_types {
101101
types.insert(file_range, expected);
102102
} else if expected.starts_with("type: ") {
103-
types.insert(file_range, expected.trim_start_matches("type: ").to_string());
103+
types.insert(file_range, expected.trim_start_matches("type: ").to_owned());
104104
} else if expected.starts_with("expected") {
105105
mismatches.insert(file_range, expected);
106106
} else if expected.starts_with("adjustments:") {
@@ -110,7 +110,7 @@ fn check_impl(ra_fixture: &str, allow_none: bool, only_types: bool, display_sour
110110
.trim_start_matches("adjustments:")
111111
.trim()
112112
.split(',')
113-
.map(|it| it.trim().to_string())
113+
.map(|it| it.trim().to_owned())
114114
.filter(|it| !it.is_empty())
115115
.collect(),
116116
);
@@ -331,7 +331,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
331331
});
332332
for (node, ty) in &types {
333333
let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.value.clone()) {
334-
(self_param.name().unwrap().syntax().text_range(), "self".to_string())
334+
(self_param.name().unwrap().syntax().text_range(), "self".to_owned())
335335
} else {
336336
(node.value.text_range(), node.value.text().to_string().replace('\n', " "))
337337
};

crates/hir-ty/src/traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ pub(crate) fn trait_solve_query(
104104
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::Implemented(it))) => {
105105
db.trait_data(it.hir_trait_id()).name.display(db.upcast()).to_string()
106106
}
107-
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(_))) => "alias_eq".to_string(),
108-
_ => "??".to_string(),
107+
GoalData::DomainGoal(DomainGoal::Holds(WhereClause::AliasEq(_))) => "alias_eq".to_owned(),
108+
_ => "??".to_owned(),
109109
};
110110
let _p = tracing::span!(tracing::Level::INFO, "trait_solve_query", ?detail).entered();
111111
tracing::info!("trait_solve_query({:?})", goal.value.goal);

crates/hir/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1933,7 +1933,7 @@ impl Function {
19331933
};
19341934
let (result, output) = interpret_mir(db, body, false, None);
19351935
let mut text = match result {
1936-
Ok(_) => "pass".to_string(),
1936+
Ok(_) => "pass".to_owned(),
19371937
Err(e) => {
19381938
let mut r = String::new();
19391939
_ = e.pretty_print(&mut r, db, &span_formatter);

crates/ide-assists/src/handlers/apply_demorgan.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext<'_>) -> Opti
9696
let dm_lhs = demorganed.lhs()?;
9797

9898
acc.add_group(
99-
&GroupLabel("Apply De Morgan's law".to_string()),
99+
&GroupLabel("Apply De Morgan's law".to_owned()),
100100
AssistId("apply_demorgan", AssistKind::RefactorRewrite),
101101
"Apply De Morgan's law",
102102
op_range,
@@ -187,7 +187,7 @@ pub(crate) fn apply_demorgan_iterator(acc: &mut Assists, ctx: &AssistContext<'_>
187187
let op_range = method_call.syntax().text_range();
188188
let label = format!("Apply De Morgan's law to `Iterator::{}`", name.text().as_str());
189189
acc.add_group(
190-
&GroupLabel("Apply De Morgan's law".to_string()),
190+
&GroupLabel("Apply De Morgan's law".to_owned()),
191191
AssistId("apply_demorgan_iterator", AssistKind::RefactorRewrite),
192192
label,
193193
op_range,

crates/ide-assists/src/handlers/convert_comment_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn block_to_line(acc: &mut Assists, comment: ast::Comment) -> Option<()> {
5757

5858
// Don't introduce trailing whitespace
5959
if line.is_empty() {
60-
line_prefix.to_string()
60+
line_prefix.to_owned()
6161
} else {
6262
format!("{line_prefix} {line}")
6363
}

crates/ide-assists/src/handlers/extract_function.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn make_function_name(semantics_scope: &hir::SemanticsScope<'_>) -> ast::NameRef
244244

245245
let default_name = "fun_name";
246246

247-
let mut name = default_name.to_string();
247+
let mut name = default_name.to_owned();
248248
let mut counter = 0;
249249
while names_in_scope.contains(&name) {
250250
counter += 1;
@@ -1949,7 +1949,7 @@ fn with_tail_expr(block: ast::BlockExpr, tail_expr: ast::Expr) -> ast::BlockExpr
19491949
}
19501950

19511951
fn format_type(ty: &hir::Type, ctx: &AssistContext<'_>, module: hir::Module) -> String {
1952-
ty.display_source_code(ctx.db(), module.into(), true).ok().unwrap_or_else(|| "_".to_string())
1952+
ty.display_source_code(ctx.db(), module.into(), true).ok().unwrap_or_else(|| "_".to_owned())
19531953
}
19541954

19551955
fn make_ty(ty: &hir::Type, ctx: &AssistContext<'_>, module: hir::Module) -> ast::Type {

crates/ide-assists/src/handlers/extract_variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
115115
let trailing_ws = if prev_ws.is_some_and(|it| it.text().starts_with('\n')) {
116116
format!("\n{indent_to}")
117117
} else {
118-
" ".to_string()
118+
" ".to_owned()
119119
};
120120

121121
ted::insert_all_raw(

0 commit comments

Comments
 (0)