Skip to content
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

"if not this separator or not that separator" misunderstanding #12633

Open
bingmatv opened this issue Apr 5, 2024 · 0 comments
Open

"if not this separator or not that separator" misunderstanding #12633

bingmatv opened this issue Apr 5, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@bingmatv
Copy link

bingmatv commented Apr 5, 2024

What it does

lint for if condition (for multiple conditions that start by !) and path separator

Advantage

No response

Drawbacks

No response

Example

use std::{io::*, path::*};
fn main() {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    if !s.ends_with('/') || !s.ends_with('\\') {
        panic!("Path must end by / or \\")
    }
    let p = Path::new(&s);
}

There's a misunderstanding in the if condition, someone may read if !s.ends_with('/') || !s.ends_with('\\') as "if not this separator or not that separator", and there's a constant MAIN_SEPARATOR in std::path, the better code may be:

use std::{io::*, path::*};
fn main() {
    let mut s = String::new();
    stdin().read_line(&mut s).unwrap();
    if !s.ends_with(MAIN_SEPARATOR) {
        panic!("Path must end by {MAIN_SEPARATOR}")
    }
    let p = Path::new(&s);
}
@bingmatv bingmatv added the A-lint Area: New lints label Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant