-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[
refurb
] Implement readlines_in_for
lint (FURB129)
- Loading branch information
Showing
9 changed files
with
393 additions
and
1 deletion.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
crates/ruff_linter/resources/test/fixtures/refurb/FURB129.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Lint | ||
import codecs | ||
import io | ||
from pathlib import Path | ||
|
||
with open('FURB129.py') as f: | ||
for _line in f.readlines(): | ||
pass | ||
a = [line.lower() for line in f.readlines()] | ||
b = {line.upper() for line in f.readlines()} | ||
c = {line.lower(): line.upper() for line in f.readlines()} | ||
|
||
with Path('FURB129.py').open() as f: | ||
for _line in f.readlines(): | ||
pass | ||
|
||
for _line in open('FURB129.py').readlines(): | ||
pass | ||
|
||
for _line in Path('FURB129.py').open().readlines(): | ||
pass | ||
|
||
|
||
def good1(): | ||
f = Path('FURB129.py').open() | ||
for _line in f.readlines(): | ||
pass | ||
f.close() | ||
|
||
|
||
def good2(f: io.BytesIO): | ||
for _line in f.readlines(): | ||
pass | ||
|
||
|
||
# False positives | ||
def bad(f): | ||
for _line in f.readlines(): | ||
pass | ||
|
||
|
||
def worse(f: codecs.StreamReader): | ||
for _line in f.readlines(): | ||
pass | ||
|
||
|
||
def foo(): | ||
class A: | ||
def readlines(self) -> list[str]: | ||
return ["a", "b", "c"] | ||
|
||
return A() | ||
|
||
|
||
for _line in foo().readlines(): | ||
pass | ||
|
||
# OK | ||
for _line in ["a", "b", "c"]: | ||
pass | ||
with open('FURB129.py') as f: | ||
for _line in f: | ||
pass | ||
for _line in f.readlines(10): | ||
pass | ||
for _not_line in f.readline(): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
crates/ruff_linter/src/rules/refurb/rules/readlines_in_for.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use ruff_diagnostics::{AlwaysFixableViolation, Diagnostic, Edit, Fix}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{Comprehension, Expr, StmtFor}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for uses of `readlines()` when iterating a file object line-by-line. | ||
/// | ||
/// ## Why is this bad? | ||
/// Instead of iterating through `list[str]` which is returned from `readlines()`, use the iteration | ||
/// through a file object which is a more convenient and performant way. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// with open("file.txt") as f: | ||
/// for line in f.readlines(): | ||
/// ... | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// with open("file.txt") as f: | ||
/// for line in f: | ||
/// ... | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: `io.IOBase.readlines`](https://docs.python.org/3/library/io.html#io.IOBase.readlines) | ||
/// - [Python documentation: methods of file objects](https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects) | ||
/// | ||
#[violation] | ||
pub(crate) struct ReadlinesInFor; | ||
|
||
impl AlwaysFixableViolation for ReadlinesInFor { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Use of readlines() in for loop") | ||
} | ||
|
||
fn fix_title(&self) -> String { | ||
"Remove readlines()".into() | ||
} | ||
} | ||
|
||
fn readlines_in_iter(checker: &mut Checker, iter_expr: &Expr) -> Option<()> { | ||
let expr_call = iter_expr.as_call_expr()?; | ||
let attr_expr = expr_call.func.as_attribute_expr()?; | ||
(attr_expr.attr.as_str() == "readlines" && expr_call.arguments.is_empty()).then(|| { | ||
checker | ||
.diagnostics | ||
.push( | ||
Diagnostic::new(ReadlinesInFor, expr_call.range()).with_fix(Fix::unsafe_edit( | ||
Edit::range_deletion( | ||
expr_call.range().add_start(attr_expr.value.range().len()), | ||
), | ||
)), | ||
); | ||
}) | ||
} | ||
|
||
// FURB129 | ||
pub(crate) fn readlines_in_for(checker: &mut Checker, for_stmt: &StmtFor) { | ||
readlines_in_iter(checker, for_stmt.iter.as_ref()); | ||
} | ||
|
||
// FURB129 | ||
pub(crate) fn readlines_in_comprehension(checker: &mut Checker, comprehension: &Comprehension) { | ||
readlines_in_iter(checker, &comprehension.iter); | ||
} |
Oops, something went wrong.