Raise `PTH201` for `Path("")` (#6095)

This commit is contained in:
Harutaka Kawamura 2023-07-26 22:22:46 +09:00 committed by GitHub
parent 77396c6f92
commit 99127243f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 16 deletions

View File

@ -5,6 +5,7 @@ from pathlib import Path as pth
_ = Path(".")
_ = pth(".")
_ = PurePath(".")
_ = Path("")
# no match
_ = Path()

View File

@ -73,13 +73,11 @@ pub(crate) fn path_constructor_current_directory(checker: &mut Checker, expr: &E
return;
};
if value != "." {
return;
}
if matches!(value.as_str(), "" | ".") {
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);
}
}

View File

@ -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`
|
@ -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
@ -38,8 +39,8 @@ 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`
|
@ -47,8 +48,7 @@ PTH201.py:7:14: PTH201 [*] Do not pass the current directory explicitly to `Path
6 | _ = pth(".")
7 | _ = PurePath(".")
| ^^^ PTH201
8 |
9 | # no match
8 | _ = Path("")
|
= help: Remove the current directory argument
@ -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()