Skip to content

Documentation about declaration shadowing incorrect. #7977

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
nibrahim opened this issue Jul 22, 2013 · 1 comment
Closed

Documentation about declaration shadowing incorrect. #7977

nibrahim opened this issue Jul 22, 2013 · 1 comment

Comments

@nibrahim
Copy link
Contributor

The documentation says

static MONSTER_FACTOR: float = 57.8;
let monster_size = MONSTER_FACTOR * 10.0;
let monster_size: int = 50;

Local variables may shadow earlier declarations, as in the previous example: > monster_size was first declared as a float, and then a second monster_size
was declared as an int. If you were to actually compile this example, though,
the compiler would determine that the first monster_size is unused and issue a > warning (because this situation is likely to indicate a programmer error)

but trying to compile

fn main() {
    static monster_factor: float = 10;
    let monster_size = monster_factor * 10.0;
    let monster_size: int = 50;
    println(fmt!("Number : %d\n", monster_size));
   }

gives me

noufal@sanitarium% rust run trial0.rs
trial0.rs:2:35: 2:37 error: mismatched types: expected float but found <VI0> > (expected float but found integral variable)
trial0.rs:2 static monster_factor: float = 10;
^~
error: aborting due to previous error

Based on the docs, I'd expect a warning about the unused variable rather than an error.

@nibrahim
Copy link
Contributor Author

This is caused due to mismatch between the 10 (which is an integer) and the type of monster_factor which is a float. Using 10.0 (or some other float as the tutorial actually suggests) prevents the problem.

flip1995 pushed a commit to flip1995/rust that referenced this issue Nov 23, 2021
Improve `needless_borrow` lint

fixes: rust-lang#5327
fixes: rust-lang#1726
fixes: rust-lang#1212

This is merging `needless_borrow` into the `dereference` pass in preparation for `explicit_auto_deref`. `explicit_auto_deref` needs to implement most of what `needless_borrow` implements in order to work.

There is a minor regression here where `let x: &str = &x.deref()` will trigger `needless_borrow` without triggering `explicit_deref_methods`. Removing the redundant borrow will cause `explicit_deref_methods` to trigger. This will be fixed when `explicit_auto_deref` is implemented.

changelog: Lint `needless_borrow` when a borrow is auto-derefed more than once
changelog: Lint `needless_borrow` in the trailing expression of a block for a match arm
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant