Skip to content

When supressing possibly-uninitialized errors, prefer showing the textually earlier error #70869

@CAD97

Description

@CAD97

I tried this code:

pub fn transliterate(mut origin: String) -> String {
    let counter: usize = origin.chars().count();
    let mut j: usize = 0;
    let mut i: usize = 0;
    let origin_vec: Vec<char> = origin.chars().collect();
    let mut origin_mutated: Vec<char>;
    if j <= counter {
        while j <= counter {
            match origin_vec[j] {
                'А' => {
                    origin_mutated[i] = 'A';
                    i += 1;
                    j += 1;
                }
                // snip
                'Я' => {
                    origin_mutated[i] = 'Y';
                    i += 1;
                    j += 1;
                    origin_mutated[i] = 'a';
                    i += 1;
                }
                _ => {
                    j += 1;
                }
            }
        }
    } else if j > counter {
        origin_mutated[i] = '\n';
    } else {
        origin = origin_mutated.into_iter().collect();
    }
    //origin = origin_mutated.into_iter().collect();
    (origin)
}

I expected to see this happen: the possibly-uninitialized error would report the textually first usage of origin_mutated

error[E0381]: borrow of possibly-uninitialized variable: `origin_mutated`
  --> src/lib.rs:11:21
   |
11 |                     origin_mutated[i] = 'A';
   |                     ^^^^^^^^^^^^^^ use of possibly-uninitialized `origin_mutated`

Instead, this happened: the possibly-uninitialized error reports the textually last usage of origin_mutated

error[E0381]: borrow of possibly-uninitialized variable: `origin_mutated`
   --> src/lib.rs:188:21
    |
188 |                     origin_mutated[i] = 'Y';
    |                     ^^^^^^^^^^^^^^ use of possibly-uninitialized `origin_mutated`

Originally reported by L0uisc on irlo.

@rustbot modify labels: +A-diagnostics, +D-papercut

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions