-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rustc recommends removing desired str method instead of adding .to_owned() #114329
Comments
Not really a clippy issue, as it's a wrapper around |
This suggestion should only be applied for methods that have no "side-effects" -- things like |
How do you determine whether the method has "side-effects" or not? Sorry if this is a stupid question. I'm an absolute newbie to rustc development, but would be keen to start on this issue. |
No, that's a fine question. The way I would do it is probably just hard-code a set of known methods. Like... |
Thanks :) That's what I was thinking as well. Is it okay if I claim this issue? I presume the error originates in type checking and hence the change would go into some HIR related code. Is that correct? |
As per my analysis the fix can go into the following rust/compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs Lines 253 to 255 in fca59ab
We can add an extra check here for methods with side effects as shown below: if let hir::ExprKind::MethodCall(hir::PathSegment { ident: method, .. }, recv_expr, &[], _) = expr.kind &&
let Some(recv_ty) = self.typeck_results.borrow().expr_ty_opt(recv_expr) &&
self.can_coerce(recv_ty, expected) &&
self.has_no_side_effects(expr) {
} where the fn has_no_side_effects(expr: &hir::Expr<'tcx>) -> bool {
if (/* expr value is &str &&
expected type is String &&
the last called method is in [sym::as_str, sym::as_ptr, sym::to_string...] */) {
true
} else {
false
}
} As a result of this change, There's a method With regard to tests, we could enhance this test to verify our case. We could also enhance this test. Or we could write completely new tests. What are your thoughts @compiler-errors? |
While filtering on a set of hard-coded method symbols may work for Perhaps a conservative, but likely unacceptable, option is to never suggest removal of methods. Then at least the suggestions in this context will always be correct although not necessarily most natural. |
I personally think that would be the best solution. I don't really care that it's suggesting I fix the code, I care that it suggests I fix it by removing a method at all that the programmer likely intended to call. Though I get that a lot of people learn Rust by letting the compiler teach them. I'm in the same boat as a learner myself. Still, seems kinda silly for the compiler to be like "Oh, you want trimming? No! The type doesn't match. Remove the trim and just deal with the spaces." when "to_owned()" handily fixes the problem. Maybe the compiler can tell you how to convert when the solution is as simple as "to_owned()"? |
Only suggest removal of `as_*` and `to_` conversion methods on E0308 Instead of ``` error[E0308]: mismatched types --> tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs:9:5 | 4 | fn get_name() -> String { | ------ expected `String` because of return type ... 9 | your_name.trim() | ^^^^^^^^^^^^^^^^ expected `String`, found `&str` | help: try removing the method call | 9 - your_name.trim() 9 + your_name ``` output ``` error[E0308]: mismatched types --> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5 | LL | fn get_name() -> String { | ------ expected `String` because of return type ... LL | your_name.trim() | ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` ``` Fix rust-lang#114329.
Only suggest removal of `as_*` and `to_` conversion methods on E0308 Instead of ``` error[E0308]: mismatched types --> tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs:9:5 | 4 | fn get_name() -> String { | ------ expected `String` because of return type ... 9 | your_name.trim() | ^^^^^^^^^^^^^^^^ expected `String`, found `&str` | help: try removing the method call | 9 - your_name.trim() 9 + your_name ``` output ``` error[E0308]: mismatched types --> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5 | LL | fn get_name() -> String { | ------ expected `String` because of return type ... LL | your_name.trim() | ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` ``` Fix rust-lang#114329.
Only suggest removal of `as_*` and `to_` conversion methods on E0308 Instead of ``` error[E0308]: mismatched types --> tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs:9:5 | 4 | fn get_name() -> String { | ------ expected `String` because of return type ... 9 | your_name.trim() | ^^^^^^^^^^^^^^^^ expected `String`, found `&str` | help: try removing the method call | 9 - your_name.trim() 9 + your_name ``` output ``` error[E0308]: mismatched types --> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5 | LL | fn get_name() -> String { | ------ expected `String` because of return type ... LL | your_name.trim() | ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` ``` Fix rust-lang#114329.
Rollup merge of rust-lang#120473 - estebank:issue-114329, r=TaKO8Ki Only suggest removal of `as_*` and `to_` conversion methods on E0308 Instead of ``` error[E0308]: mismatched types --> tests/ui/suggestions/only-suggest-removal-of-conversion-method-calls.rs:9:5 | 4 | fn get_name() -> String { | ------ expected `String` because of return type ... 9 | your_name.trim() | ^^^^^^^^^^^^^^^^ expected `String`, found `&str` | help: try removing the method call | 9 - your_name.trim() 9 + your_name ``` output ``` error[E0308]: mismatched types --> $DIR/only-suggest-removal-of-conversion-method-calls.rs:9:5 | LL | fn get_name() -> String { | ------ expected `String` because of return type ... LL | your_name.trim() | ^^^^^^^^^^^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` ``` Fix rust-lang#114329.
Summary
When you have a function that returns a String but add
.trim()
to the return value to remove extraneous whitespace, you have a type error because.trim()
returns a&str
. But the suggestion is not to add.to_owned()
orto_string()
to fix the problem but to remove the trimming behavior entirely even if that is exactly what the programmer intended.When using cargo run, it additionally fails to show the line containing .trim() in the suggested change.
Lint Name
error[E0308]: mismatched types
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen:
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: