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

Raise PTH201 for Path("") #6095

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
_ = Path(".")
_ = pth(".")
_ = PurePath(".")
_ = Path("")

# no match
_ = Path()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(crate) fn path_constructor_current_directory(checker: &mut Checker, expr: &E
return;
};

if value != "." {
if !value.is_empty() && value != "." {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if matches!(value, "" | ".") { let mut diagnostic ... } ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it a bit easier to read since it encodes that exact patterns we care about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, thanks for the suggestion!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    if value.is_empty() || value == "." {
        let mut diagnostic = Diagnostic::new(PathConstructorCurrentDirectory, *range);
        if checker.patch(diagnostic.kind.rule()) {
            diagnostic.set_fix(Fix::automatic(Edit::range_deletion(*range)));
        }
        checker.diagnostics.push(diagnostic);
    }

Is this easier to read?

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ PTH201.py:5:10: PTH201 [*] Do not pass the current directory explicitly to `Path
5 |+_ = Path()
6 6 | _ = pth(".")
7 7 | _ = PurePath(".")
8 8 |
8 8 | _ = Path("")

PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
Expand All @@ -28,6 +28,7 @@ PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path`
6 | _ = pth(".")
| ^^^ PTH201
7 | _ = PurePath(".")
8 | _ = Path("")
|
= help: Remove the current directory argument

Expand All @@ -38,17 +39,16 @@ PTH201.py:6:9: PTH201 [*] Do not pass the current directory explicitly to `Path`
6 |-_ = pth(".")
6 |+_ = pth()
7 7 | _ = PurePath(".")
8 8 |
9 9 | # no match
8 8 | _ = Path("")
9 9 |

PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
5 | _ = Path(".")
6 | _ = pth(".")
7 | _ = PurePath(".")
| ^^^ PTH201
8 |
9 | # no match
8 | _ = Path("")
|
= help: Remove the current directory argument

Expand All @@ -58,8 +58,29 @@ PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path
6 6 | _ = pth(".")
7 |-_ = PurePath(".")
7 |+_ = PurePath()
8 8 |
9 9 | # no match
10 10 | _ = Path()
8 8 | _ = Path("")
9 9 |
10 10 | # no match

PTH201.py:8:10: PTH201 [*] Do not pass the current directory explicitly to `Path`
|
6 | _ = pth(".")
7 | _ = PurePath(".")
8 | _ = Path("")
| ^^ PTH201
9 |
10 | # no match
|
= help: Remove the current directory argument

ℹ Fix
5 5 | _ = Path(".")
6 6 | _ = pth(".")
7 7 | _ = PurePath(".")
8 |-_ = Path("")
8 |+_ = Path()
9 9 |
10 10 | # no match
11 11 | _ = Path()


Loading