Skip to content

Commit

Permalink
Ignore multiply-assigned variables in RET504
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 8, 2023
1 parent 3f04def commit a420955
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 89 deletions.
75 changes: 21 additions & 54 deletions crates/ruff/resources/test/fixtures/flake8_return/RET504.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,6 @@ def x():
return a # error


def x():
a = 1
print(a)
a = 2
return a # error


def x():
a = 1
if True:
return a # error
a = 2
print(a)
return a


# Can be refactored false positives
# https://github.com/afonasev/flake8-return/issues/47#issuecomment-1122571066
def get_bar_if_exists(obj):
Expand Down Expand Up @@ -165,44 +149,6 @@ def my_func():
return foo


# Refactored incorrect false positives
# See test cases above marked: "Can be refactored false positives"
# https://github.com/afonasev/flake8-return/issues/47#issuecomment-1122571066
def get_bar_if_exists(obj):
if hasattr(obj, "bar"):
return str(obj.bar)
return ""


# https://github.com/afonasev/flake8-return/issues/47#issue-641117366
def x():
formatted = _USER_AGENT_FORMATTER.format(format_string, **values)
# clean up after any blank components
return formatted.replace("()", "").replace(" ", " ").strip()


# https://github.com/afonasev/flake8-return/issues/47#issue-641117366
def user_agent_username(username=None):

if not username:
return ""

username = username.replace(" ", "_") # Avoid spaces or %20.
try:
username.encode("ascii") # just test,
# but not actually use it
except UnicodeEncodeError:
username = quote(username.encode("utf-8"))
else:
# % is legal in the default $wgLegalTitleChars
# This is so that ops know the real pywikibot will not
# allow a useragent in the username to allow through a
# hand-coded percent-encoded value.
if "%" in username:
username = quote(username)
return username


# https://github.com/afonasev/flake8-return/issues/116#issue-1367575481
def no_exception_loop():
success = False
Expand Down Expand Up @@ -260,3 +206,24 @@ def inner():
nonlocal X
X = 1
return X


def get_queryset(option_1, option_2):
queryset: Any = None
queryset = queryset.filter(a=1)
if option_1:
queryset = queryset.annotate(b=Value(2))
if option_2:
queryset = queryset.filter(c=3)
return queryset


def get_queryset():
queryset = Model.filter(a=1)
queryset = queryset.filter(c=3)
return queryset


def get_queryset():
queryset = Model.filter(a=1)
return queryset # error
12 changes: 11 additions & 1 deletion crates/ruff/src/rules/flake8_return/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ fn implicit_return(checker: &mut Checker, stmt: &Stmt) {
}
}

fn has_multiple_assigns(id: &str, stack: &Stack) -> bool {
if let Some(assigns) = stack.assigns.get(&id) {
if assigns.len() > 1 {
return true;
}
}
false
}

fn has_refs_before_next_assign(id: &str, return_location: Location, stack: &Stack) -> bool {
let mut before_assign: &Location = &Location::default();
let mut after_assign: Option<&Location> = None;
Expand Down Expand Up @@ -390,7 +399,8 @@ fn unnecessary_assign(checker: &mut Checker, stack: &Stack, expr: &Expr) {
return;
}

if has_refs_before_next_assign(id, expr.location, stack)
if has_multiple_assigns(id, stack)
|| has_refs_before_next_assign(id, expr.location, stack)
|| has_refs_or_assigns_within_try_or_loop(id, stack)
{
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
source: src/rules/flake8_return/mod.rs
source: crates/ruff/src/rules/flake8_return/mod.rs
expression: diagnostics
---
- kind:
Expand All @@ -15,41 +15,11 @@ expression: diagnostics
- kind:
UnnecessaryAssign: ~
location:
row: 13
row: 229
column: 11
end_location:
row: 13
column: 12
fix: ~
parent: ~
- kind:
UnnecessaryAssign: ~
location:
row: 19
column: 15
end_location:
row: 19
column: 16
fix: ~
parent: ~
- kind:
UnnecessaryAssign: ~
location:
row: 31
column: 11
end_location:
row: 31
column: 17
fix: ~
parent: ~
- kind:
UnnecessaryAssign: ~
location:
row: 39
column: 11
end_location:
row: 39
column: 20
row: 229
column: 19
fix: ~
parent: ~

0 comments on commit a420955

Please sign in to comment.