Skip to content

Conversation

JohnCSimon
Copy link

@JohnCSimon JohnCSimon commented Mar 17, 2019

Hi! I'm new-ish to rust and I noticed the Clippy linter had several things to say about the code for ls, so, here is a PR taking in a number of its suggestions

also the code for pad left

fn pad_left(string: String, count: usize) -> String {
    if count > string.len() {
        let pad = count - string.len();
        let pad = String::from_utf8(vec![' ' as u8; pad]).unwrap();
        format!("{}{}", pad, string)
    } else {
        string
    }
}

has a suggestion o String::from_utf8(vec![' ' as u8; pad]).

casting character literal to u8. `char`s are 4 bytes wide in rust, so casting to u8 truncates them

note: #[warn(clippy::char_lit_as_u8)] on by default
help: Consider using a byte literal instead:
      b' '
help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#char_lit_as_u8clippy(clippy::char_lit_as_u8)

was this intended?

@Arcterus
Copy link
Collaborator

Arcterus commented Apr 6, 2019

I believe that was intended, but that may have been written before byte literals existed. Anyway, that line could be changed to:

let pad = " ".repeat(pad);

Actually, you might be able to do something like this to add padding too (not tested):

format!("{: width$}", string, width = pad)

@rivy
Copy link
Member

rivy commented Apr 14, 2020

Thanks for the contribution!
The fix for this same issue was already included in a large lint and CICD fixup PR (#1449).
We'd be happy to see you back with new contributions in the future.

@rivy rivy closed this Apr 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants