Skip to content

Commit

Permalink
Improve some rule messages and docs (#14068)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh authored Nov 3, 2024
1 parent f09dc8b commit 2b0cdd2
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl AlwaysFixableViolation for DecimalFromFloatLiteral {
}

fn fix_title(&self) -> String {
"Use a string literal instead".to_string()
"Replace with string literal".to_string()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ impl AlwaysFixableViolation for IncorrectlyParenthesizedTupleInSubscript {
#[derive_message_formats]
fn message(&self) -> String {
if self.prefer_parentheses {
format!("Use parentheses for tuples in subscripts.")
format!("Use parentheses for tuples in subscripts")
} else {
format!("Avoid parentheses for tuples in subscripts.")
format!("Avoid parentheses for tuples in subscripts")
}
}

fn fix_title(&self) -> String {
if self.prefer_parentheses {
"Parenthesize the tuple.".to_string()
"Parenthesize tuple".to_string()
} else {
"Remove the parentheses.".to_string()
"Remove parentheses".to_string()
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/ruff_linter/src/rules/ruff/rules/useless_if_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use ruff_python_ast as ast;
use ruff_python_ast::comparable::ComparableExpr;

/// ## What it does
/// Checks for useless if-else conditions with identical arms.
/// Checks for useless `if`-`else` conditions with identical arms.
///
/// ## Why is this bad?
/// Useless if-else conditions add unnecessary complexity to the code without
/// Useless `if`-`else` conditions add unnecessary complexity to the code without
/// providing any logical benefit.
///
/// Assigning the value directly is clearer and more explicit, and
Expand All @@ -31,11 +31,11 @@ pub struct UselessIfElse;
impl Violation for UselessIfElse {
#[derive_message_formats]
fn message(&self) -> String {
format!("Useless if-else condition")
format!("Useless `if`-`else` condition")
}
}

/// RUF031
/// RUF034
pub(crate) fn useless_if_else(checker: &mut Checker, if_expr: &ast::ExprIf) {
let ast::ExprIf {
body,
Expand All @@ -44,7 +44,7 @@ pub(crate) fn useless_if_else(checker: &mut Checker, if_expr: &ast::ExprIf) {
..
} = if_expr;

// Skip if the body and orelse are not the same
// Skip if the `body` and `orelse` are not the same.
if ComparableExpr::from(body) != ComparableExpr::from(orelse) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF031.py:2:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:2:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
1 | d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"}
2 | d[(1,2)]
| ^^^^^ RUF031
3 | d[(
4 | 1,
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
1 1 | d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"}
Expand All @@ -19,7 +19,7 @@ RUF031.py:2:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
4 4 | 1,
5 5 | 2

RUF031.py:3:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:3:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
1 | d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"}
2 | d[(1,2)]
Expand All @@ -32,7 +32,7 @@ RUF031.py:3:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
7 | d[
8 | 1,
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
1 1 | d = {(1,2):"a",(3,4):"b",(5,6,7):"c",(8,):"d"}
Expand All @@ -47,7 +47,7 @@ RUF031.py:3:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
8 8 | 1,
9 9 | 2

RUF031.py:11:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:11:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
9 | 2
10 | ]
Expand All @@ -56,7 +56,7 @@ RUF031.py:11:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
12 | d[(5,6,7)]
13 | d[(8,)]
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
8 8 | 1,
Expand All @@ -68,7 +68,7 @@ RUF031.py:11:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
13 13 | d[(8,)]
14 14 | d[tuple(1,2)]

RUF031.py:12:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:12:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
10 | ]
11 | d[(2,4)]
Expand All @@ -77,7 +77,7 @@ RUF031.py:12:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
13 | d[(8,)]
14 | d[tuple(1,2)]
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
9 9 | 2
Expand All @@ -89,7 +89,7 @@ RUF031.py:12:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
14 14 | d[tuple(1,2)]
15 15 | d[tuple(8)]

RUF031.py:13:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:13:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
11 | d[(2,4)]
12 | d[(5,6,7)]
Expand All @@ -98,7 +98,7 @@ RUF031.py:13:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
14 | d[tuple(1,2)]
15 | d[tuple(8)]
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
10 10 | ]
Expand All @@ -110,15 +110,15 @@ RUF031.py:13:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
15 15 | d[tuple(8)]
16 16 | d[1,2]

RUF031.py:20:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:20:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
18 | d[5,6,7]
19 | e = {((1,2),(3,4)):"a"}
20 | e[((1,2),(3,4))]
| ^^^^^^^^^^^^^ RUF031
21 | e[(1,2),(3,4)]
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
17 17 | d[3,4]
Expand All @@ -131,14 +131,14 @@ RUF031.py:20:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
23 23 | token_features[
24 24 | (window_position, feature_name)

RUF031.py:24:5: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:24:5: RUF031 [*] Avoid parentheses for tuples in subscripts
|
23 | token_features[
24 | (window_position, feature_name)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF031
25 | ] = self._extract_raw_features_from_token
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
21 21 | e[(1,2),(3,4)]
Expand All @@ -150,15 +150,15 @@ RUF031.py:24:5: RUF031 [*] Avoid parentheses for tuples in subscripts.
26 26 |
27 27 | d[1,]

RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
27 | d[1,]
28 | d[(1,)]
| ^^^^ RUF031
29 | d[()] # empty tuples should be ignored
30 | d[:,] # slices in the subscript lead to syntax error if parens are added
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
25 25 | ] = self._extract_raw_features_from_token
Expand All @@ -170,7 +170,7 @@ RUF031.py:28:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
30 30 | d[:,] # slices in the subscript lead to syntax error if parens are added
31 31 | d[1,2,:]

RUF031.py:36:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
RUF031.py:36:3: RUF031 [*] Avoid parentheses for tuples in subscripts
|
34 | # Python <=3.10 to avoid syntax error.
35 | # https://github.com/astral-sh/ruff/issues/12776
Expand All @@ -179,7 +179,7 @@ RUF031.py:36:3: RUF031 [*] Avoid parentheses for tuples in subscripts.
37 |
38 | x: dict[str, int] # tuples inside type annotations should never be altered
|
= help: Remove the parentheses.
= help: Remove parentheses

Safe fix
33 33 | # Should keep these parentheses in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ RUF032.py:6:17: RUF032 [*] `Decimal()` called with float literal argument
7 |
8 | decimal.Decimal("0.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
3 3 | # Tests with fully qualified import
Expand All @@ -31,7 +31,7 @@ RUF032.py:12:17: RUF032 [*] `Decimal()` called with float literal argument
13 |
14 | decimal.Decimal("10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
9 9 |
Expand All @@ -52,7 +52,7 @@ RUF032.py:18:17: RUF032 [*] `Decimal()` called with float literal argument
19 |
20 | decimal.Decimal("-10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
15 15 |
Expand All @@ -73,7 +73,7 @@ RUF032.py:33:15: RUF032 [*] `Decimal()` called with float literal argument
34 |
35 | val = Decimal("0.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
30 30 |
Expand All @@ -94,7 +94,7 @@ RUF032.py:39:15: RUF032 [*] `Decimal()` called with float literal argument
40 |
41 | val = Decimal("10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
36 36 |
Expand All @@ -115,7 +115,7 @@ RUF032.py:45:15: RUF032 [*] `Decimal()` called with float literal argument
46 |
47 | val = Decimal("-10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
42 42 |
Expand All @@ -136,7 +136,7 @@ RUF032.py:56:15: RUF032 [*] `Decimal()` called with float literal argument
57 |
58 | val = Decimal(-+--++--4.0) # Suggest `Decimal("-4.0")`
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
53 53 | # See https://github.com/astral-sh/ruff/issues/13258
Expand All @@ -155,7 +155,7 @@ RUF032.py:58:15: RUF032 [*] `Decimal()` called with float literal argument
58 | val = Decimal(-+--++--4.0) # Suggest `Decimal("-4.0")`
| ^^^^^^^^^^^ RUF032
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
55 55 |
Expand All @@ -176,7 +176,7 @@ RUF032.py:88:23: RUF032 [*] `Decimal()` called with float literal argument
89 |
90 | val = decimal.Decimal("0.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
85 85 |
Expand All @@ -197,7 +197,7 @@ RUF032.py:92:23: RUF032 [*] `Decimal()` called with float literal argument
93 |
94 | val = decimal.Decimal("10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
89 89 |
Expand All @@ -218,7 +218,7 @@ RUF032.py:96:23: RUF032 [*] `Decimal()` called with float literal argument
97 |
98 | val = decimal.Decimal("-10.0")
|
= help: Use a string literal instead
= help: Replace with string literal

Unsafe fix
93 93 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
---
RUF034.py:5:5: RUF034 Useless if-else condition
RUF034.py:5:5: RUF034 Useless `if`-`else` condition
|
4 | # Invalid
5 | x = 1 if True else 1
Expand All @@ -10,7 +10,7 @@ RUF034.py:5:5: RUF034 Useless if-else condition
7 | # Invalid
|

RUF034.py:8:5: RUF034 Useless if-else condition
RUF034.py:8:5: RUF034 Useless `if`-`else` condition
|
7 | # Invalid
8 | x = "a" if True else "a"
Expand All @@ -19,7 +19,7 @@ RUF034.py:8:5: RUF034 Useless if-else condition
10 | # Invalid
|

RUF034.py:11:5: RUF034 Useless if-else condition
RUF034.py:11:5: RUF034 Useless `if`-`else` condition
|
10 | # Invalid
11 | x = 0.1 if False else 0.1
Expand Down
Loading

0 comments on commit 2b0cdd2

Please sign in to comment.