-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Compiler could be more helpful #116738
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Comments
3tilley
added
A-diagnostics
Area: Messages for errors, warnings, and lints
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
labels
Oct 14, 2023
rustbot
added
the
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
label
Oct 14, 2023
fmease
added
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
and removed
needs-triage
This issue may need triage. Remove it if it has been sufficiently triaged.
labels
Oct 16, 2023
For anyone may want to resolve this issue, I made a sample without the crate: use std::path::Path;
struct File {
path: String,
}
impl File {
fn new(path: &Path) -> Self {
Self { path: path.to_str().unwrap().to_string() }
}
}
fn open(path: &Path) -> Option<File> {
Some(File::new(&path))
}
trait FileAPI {
fn read(self) -> String;
}
impl FileAPI for File {
fn read(self) -> String {
std::fs::read_to_string(&self.path).unwrap()
}
}
fn test_file<T>(f: T) -> String
where
T: FileAPI,
{
f.read()
}
fn main() {
let path = Path::new(file!()).parent().unwrap();
let file = open(path);
let _ = test_file(file);
} |
matthiaskrgr
added a commit
to matthiaskrgr/rust
that referenced
this issue
Oct 24, 2023
…xpect, r=b-naber Suggest unwrap/expect for let binding type mismatch Found it when investigating rust-lang#116738 I'm not sure whether it's a good style to suggest `unwrap`, seems it's may helpful for newcomers. rust-lang#116738 needs another fix to improve it.
rust-timer
added a commit
to rust-lang-ci/rust
that referenced
this issue
Oct 24, 2023
Rollup merge of rust-lang#116841 - chenyukang:yukang-suggest-unwrap-expect, r=b-naber Suggest unwrap/expect for let binding type mismatch Found it when investigating rust-lang#116738 I'm not sure whether it's a good style to suggest `unwrap`, seems it's may helpful for newcomers. rust-lang#116738 needs another fix to improve it.
@chenyukang are you working on this or can I claim? |
@3tilley you can work on it, please go ahead. |
@rustbot claim |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
D-newcomer-roadblock
Diagnostics: Confusing error or lint; hard to understand for new users.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Code
Cargo.toml
main.rs
Current output
Desired output
The error occurs because I forgot to deal with the Result, but I think the compiler could suggest here that
.expect
could be called here as I've seen in other situations. It seems possible to suggest that ifResult<T,V>
fails a trait requirement butT
passes then this could be an incident ofResult
forgetfulness.Rationale and extra context
This would only be checked in case of failure, so I don't think it would add overhead to the compilation time?
Other cases
I checked with
std::fs
andfs-err
just in case, the output was the sameAnything else?
No response
The text was updated successfully, but these errors were encountered: