Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
use clippy_utils::res::{MaybeDef, MaybeQPath};
use clippy_utils::source::{snippet, snippet_with_applicability};
use clippy_utils::source::{snippet, snippet_with_applicability, snippet_with_context};
use clippy_utils::{
SpanlessEq, get_expr_use_or_unification_node, get_parent_expr, is_lint_allowed, method_calls, peel_blocks, sym,
};
Expand Down Expand Up @@ -404,7 +404,8 @@ impl<'tcx> LateLintPass<'tcx> for StrToString {
"`to_string()` called on a `&str`",
|diag| {
let mut applicability = Applicability::MachineApplicable;
let snippet = snippet_with_applicability(cx, self_arg.span, "..", &mut applicability);
let (snippet, _) =
snippet_with_context(cx, self_arg.span, expr.span.ctxt(), "..", &mut applicability);
diag.span_suggestion(expr.span, "try", format!("{snippet}.to_owned()"), applicability);
},
);
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/str_to_string.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ fn main() {
msg.to_owned();
//~^ str_to_string
}

fn issue16271(key: &[u8]) {
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}

let _value = t!(str::from_utf8(key)).to_owned();
//~^ str_to_string
}
14 changes: 14 additions & 0 deletions tests/ui/str_to_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ fn main() {
msg.to_string();
//~^ str_to_string
}

fn issue16271(key: &[u8]) {
macro_rules! t {
($e:expr) => {
match $e {
Ok(e) => e,
Err(e) => panic!("{} failed with {}", stringify!($e), e),
}
};
}

let _value = t!(str::from_utf8(key)).to_string();
//~^ str_to_string
}
8 changes: 7 additions & 1 deletion tests/ui/str_to_string.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,11 @@ error: `to_string()` called on a `&str`
LL | msg.to_string();
| ^^^^^^^^^^^^^^^ help: try: `msg.to_owned()`

error: aborting due to 2 previous errors
error: `to_string()` called on a `&str`
--> tests/ui/str_to_string.rs:22:18
|
LL | let _value = t!(str::from_utf8(key)).to_string();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `t!(str::from_utf8(key)).to_owned()`

error: aborting due to 3 previous errors

Loading