Safe Rust guarantees are primarily around memory safety and race conditions, not logic errors in general. So sure, Rust will compile code with logic errors -- and the same is true of every other programming language. The language can't omnipotently know what the program is supposed to do.
Not necessarily, maybe they just wrote more code that necessary because they didn't realize a certain case was impossible, or got some privacy annotations wrong and didn't expose some function that was supposed to be public.
Because enum Location variant Range have two numbers I assume this numbers means min and max range.
So, line Location::Range(_, n) => n, will return max number of Range. But at time min is 0 it should return 0, but this line is unreachable.
Great. You are only missing one key piece: Patterns are evaluated in order.
In the same way, values go through each pattern in a match, and at the first pattern the value βfits,β the value falls into the associated code block to be used during execution.
I think you misunderstood me. I know that locations after Location::Range(_, n) => n, is unreachable because this line fits all possible Range variants, so it should be at the end of arms.
I am only asking why Rust treats unreachable code as warning and not as error.
As quinedot wrote it is logical error, but this type of logical error is caught and most of time means a bug.
I think this type of bug should be treated the same as not exhausting all arms.
If you use not all arms (this is error in Rust) or too many arms (because of unreachable warning) it should be error and not a warning.
Because it is not necessarily an error. If you declare a variable and never use it, that's also dead code. It's not an error. It is treated as such in Go, and that annoys the hell out if people trying to debug their code by commenting out parts of it.
If you were to write the following function, should Rust (or whatever programming language you are used to) reject it just because you were expecting it to return 1 instead of 0?
fn what(var: usize) -> usize {
if var >= 3 {
return 0;
}
return 1;
}
let result = what(3);
Here is the reasoning why it's a warning and not an error:
Dead code: this is a lint. While the user probably doesn't want dead code in their crate, making this a hard error would make refactoring and development very painful.