Skip to content

Commit

Permalink
Quote function
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Feb 13, 2024
1 parent a3d8ee6 commit d9e6d77
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
42 changes: 22 additions & 20 deletions crates/ruff_linter/src/rules/refurb/rules/readlines_in_for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,38 @@ pub(crate) struct ReadlinesInFor;
impl AlwaysFixableViolation for ReadlinesInFor {
#[derive_message_formats]
fn message(&self) -> String {
format!("Use of readlines() in for loop")
format!("Use of `readlines()` in loop")
}

fn fix_title(&self) -> String {
"Remove readlines()".into()
"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
/// FURB129
pub(crate) fn readlines_in_for(checker: &mut Checker, for_stmt: &StmtFor) {
readlines_in_iter(checker, for_stmt.iter.as_ref());
}

// FURB129
/// FURB129
pub(crate) fn readlines_in_comprehension(checker: &mut Checker, comprehension: &Comprehension) {
readlines_in_iter(checker, &comprehension.iter);
}

fn readlines_in_iter(checker: &mut Checker, iter_expr: &Expr) {
let Expr::Call(expr_call) = iter_expr else {
return;
};

let Expr::Attribute(expr_attr) = expr_call.func.as_ref() else {
return;
};

if expr_attr.attr.as_str() == "readlines" && expr_call.arguments.is_empty() {
let mut diagnostic = Diagnostic::new(ReadlinesInFor, expr_call.range());
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_deletion(
expr_call.range().add_start(expr_attr.value.range().len()),
)));
checker.diagnostics.push(diagnostic);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/refurb/mod.rs
---
FURB129.py:7:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:7:18: FURB129 [*] Use of `readlines()` in loop
|
5 | # Errors
6 | with open("FURB129.py") as f:
Expand All @@ -10,7 +10,7 @@ FURB129.py:7:18: FURB129 [*] Use of readlines() in for loop
8 | pass
9 | a = [line.lower() for line in f.readlines()]
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
4 4 |
Expand All @@ -22,7 +22,7 @@ FURB129.py:7:18: FURB129 [*] Use of readlines() in for loop
9 9 | a = [line.lower() for line in f.readlines()]
10 10 | b = {line.upper() for line in f.readlines()}

FURB129.py:9:35: FURB129 [*] Use of readlines() in for loop
FURB129.py:9:35: FURB129 [*] Use of `readlines()` in loop
|
7 | for _line in f.readlines():
8 | pass
Expand All @@ -31,7 +31,7 @@ FURB129.py:9:35: FURB129 [*] Use of readlines() in for loop
10 | b = {line.upper() for line in f.readlines()}
11 | c = {line.lower(): line.upper() for line in f.readlines()}
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
6 6 | with open("FURB129.py") as f:
Expand All @@ -43,15 +43,15 @@ FURB129.py:9:35: FURB129 [*] Use of readlines() in for loop
11 11 | c = {line.lower(): line.upper() for line in f.readlines()}
12 12 |

FURB129.py:10:35: FURB129 [*] Use of readlines() in for loop
FURB129.py:10:35: FURB129 [*] Use of `readlines()` in loop
|
8 | pass
9 | a = [line.lower() for line in f.readlines()]
10 | b = {line.upper() for line in f.readlines()}
| ^^^^^^^^^^^^^ FURB129
11 | c = {line.lower(): line.upper() for line in f.readlines()}
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
7 7 | for _line in f.readlines():
Expand All @@ -63,7 +63,7 @@ FURB129.py:10:35: FURB129 [*] Use of readlines() in for loop
12 12 |
13 13 | with Path("FURB129.py").open() as f:

FURB129.py:11:49: FURB129 [*] Use of readlines() in for loop
FURB129.py:11:49: FURB129 [*] Use of `readlines()` in loop
|
9 | a = [line.lower() for line in f.readlines()]
10 | b = {line.upper() for line in f.readlines()}
Expand All @@ -72,7 +72,7 @@ FURB129.py:11:49: FURB129 [*] Use of readlines() in for loop
12 |
13 | with Path("FURB129.py").open() as f:
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
8 8 | pass
Expand All @@ -84,14 +84,14 @@ FURB129.py:11:49: FURB129 [*] Use of readlines() in for loop
13 13 | with Path("FURB129.py").open() as f:
14 14 | for _line in f.readlines():

FURB129.py:14:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:14:18: FURB129 [*] Use of `readlines()` in loop
|
13 | with Path("FURB129.py").open() as f:
14 | for _line in f.readlines():
| ^^^^^^^^^^^^^ FURB129
15 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
11 11 | c = {line.lower(): line.upper() for line in f.readlines()}
Expand All @@ -103,15 +103,15 @@ FURB129.py:14:18: FURB129 [*] Use of readlines() in for loop
16 16 |
17 17 | for _line in open("FURB129.py").readlines():

FURB129.py:17:14: FURB129 [*] Use of readlines() in for loop
FURB129.py:17:14: FURB129 [*] Use of `readlines()` in loop
|
15 | pass
16 |
17 | for _line in open("FURB129.py").readlines():
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB129
18 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
14 14 | for _line in f.readlines():
Expand All @@ -123,15 +123,15 @@ FURB129.py:17:14: FURB129 [*] Use of readlines() in for loop
19 19 |
20 20 | for _line in Path("FURB129.py").open().readlines():

FURB129.py:20:14: FURB129 [*] Use of readlines() in for loop
FURB129.py:20:14: FURB129 [*] Use of `readlines()` in loop
|
18 | pass
19 |
20 | for _line in Path("FURB129.py").open().readlines():
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FURB129
21 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
17 17 | for _line in open("FURB129.py").readlines():
Expand All @@ -143,7 +143,7 @@ FURB129.py:20:14: FURB129 [*] Use of readlines() in for loop
22 22 |
23 23 |

FURB129.py:26:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:26:18: FURB129 [*] Use of `readlines()` in loop
|
24 | def good1():
25 | f = Path("FURB129.py").open()
Expand All @@ -152,7 +152,7 @@ FURB129.py:26:18: FURB129 [*] Use of readlines() in for loop
27 | pass
28 | f.close()
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
23 23 |
Expand All @@ -164,14 +164,14 @@ FURB129.py:26:18: FURB129 [*] Use of readlines() in for loop
28 28 | f.close()
29 29 |

FURB129.py:32:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:32:18: FURB129 [*] Use of `readlines()` in loop
|
31 | def good2(f: io.BytesIO):
32 | for _line in f.readlines():
| ^^^^^^^^^^^^^ FURB129
33 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
29 29 |
Expand All @@ -183,15 +183,15 @@ FURB129.py:32:18: FURB129 [*] Use of readlines() in for loop
34 34 |
35 35 |

FURB129.py:38:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:38:18: FURB129 [*] Use of `readlines()` in loop
|
36 | # False positives
37 | def bad(f):
38 | for _line in f.readlines():
| ^^^^^^^^^^^^^ FURB129
39 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
35 35 |
Expand All @@ -203,14 +203,14 @@ FURB129.py:38:18: FURB129 [*] Use of readlines() in for loop
40 40 |
41 41 |

FURB129.py:43:18: FURB129 [*] Use of readlines() in for loop
FURB129.py:43:18: FURB129 [*] Use of `readlines()` in loop
|
42 | def worse(f: codecs.StreamReader):
43 | for _line in f.readlines():
| ^^^^^^^^^^^^^ FURB129
44 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
40 40 |
Expand All @@ -222,13 +222,13 @@ FURB129.py:43:18: FURB129 [*] Use of readlines() in for loop
45 45 |
46 46 |

FURB129.py:55:14: FURB129 [*] Use of readlines() in for loop
FURB129.py:55:14: FURB129 [*] Use of `readlines()` in loop
|
55 | for _line in foo().readlines():
| ^^^^^^^^^^^^^^^^^ FURB129
56 | pass
|
= help: Remove readlines()
= help: Remove `readlines()`

Unsafe fix
52 52 | return A()
Expand Down

0 comments on commit d9e6d77

Please sign in to comment.