-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggest using slice when encountering let x = ""[..];
#46249
Conversation
r? @eddyb (rust_highfive has picked a reviewer for you, use r? to override) |
I don't think the index type matters, we should just not check it and just emit the suggestion. |
|
||
fn main() { | ||
let s = "abc"; | ||
let t = if true { s[..2] } else { s }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to add //~ ERROR
comments. Ui tests work like compile-fail now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@@ -821,6 +823,33 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { | |||
err.emit(); | |||
} | |||
|
|||
/// When encountering an assignment of an unsized trait, like `let x = ""[..];`, provide a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why just for assignments and not all the cases in the test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other cases in the test go through type check instead. I'll see if I can expand #46256 to also suggest in these cases. I included all the cases originally mentioned in order to make sure we don't regress in any of the cases (without noticing).
134889a
to
3b56558
Compare
3b56558
to
97d8d04
Compare
@eddyb I believe you were right so removed the index type check. |
r? @nikomatsakis I still feel this is much more specific than it should be, but I don't have any good suggestions, other than perhaps that In general, both deref and indexing could use a suggestion to borrow the resulting lvalue. |
--> $DIR/str-array-assignment.rs:17:27 | ||
| | ||
11 | fn main() { //~ NOTE expected `()` because of default return type | ||
| - expected `()` because of default return type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks dubious. Could you look into this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I noticed that too. Still digging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's also already the case on nightly. Feel free to ignore this and open an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This feels hacky to me, but I don't see any better way of implementing this (the rhs of locals is an lvalue, so checks based on it being an rvalue won't quite work). btw, you should be checking that the problem type is a slice or string - if the return type is a struct or trait object, you don't want the error message to mention "slices". |
OTOH, having an |
c924646
to
fa44927
Compare
@arielb1 reworded. |
@bors r+ rollup |
📌 Commit fa44927 has been approved by |
Suggest using slice when encountering `let x = ""[..];` Fix rust-lang#26319.
Fix #26319.