---
source: crates/ruff_linter/src/rules/flake8_return/mod.rs
---
RET505.py:8:5: RET505 [*] Unnecessary `elif` after `return` statement
   |
 6 |         a = 1
 7 |         return y
 8 |     elif z:
   |     ^^^^ RET505
 9 |         b = 2
10 |         return w
   |
   = help: Remove unnecessary `elif`

ℹ Safe fix
5 5 |     if x:  # [no-else-return]
6 6 |         a = 1
7 7 |         return y
8   |-    elif z:
  8 |+    if z:
9 9 |         b = 2
10 10 |         return w
11 11 |     else:

RET505.py:23:5: RET505 [*] Unnecessary `elif` after `return` statement
   |
21 |             b = 2
22 |         return
23 |     elif z:
   |     ^^^^ RET505
24 |         c = 2
25 |     else:
   |
   = help: Remove unnecessary `elif`

ℹ Safe fix
20 20 |         else:
21 21 |             b = 2
22 22 |         return
23    |-    elif z:
   23 |+    if z:
24 24 |         c = 2
25 25 |     else:
26 26 |         c = 3

RET505.py:41:5: RET505 [*] Unnecessary `elif` after `return` statement
   |
39 |         a = 1
40 |         return y
41 |     elif z:
   |     ^^^^ RET505
42 |         b = 2
43 |         return w
   |
   = help: Remove unnecessary `elif`

ℹ Safe fix
38 38 |     if x:  # [no-else-return]
39 39 |         a = 1
40 40 |         return y
41    |-    elif z:
   41 |+    if z:
42 42 |         b = 2
43 43 |         return w
44 44 |     else:

RET505.py:53:5: RET505 [*] Unnecessary `else` after `return` statement
   |
51 |         a = 1
52 |         return y
53 |     else:
   |     ^^^^ RET505
54 |         b = 2
55 |         return z
   |
   = help: Remove unnecessary `else`

ℹ Safe fix
50 50 |     if x:  # [no-else-return]
51 51 |         a = 1
52 52 |         return y
53    |-    else:
54    |-        b = 2
55    |-        return z
   53 |+    b = 2
   54 |+    return z
56 55 | 
57 56 | 
58 57 | def foo3(x, y, z):

RET505.py:64:9: RET505 [*] Unnecessary `else` after `return` statement
   |
62 |             b = 2
63 |             return y
64 |         else:
   |         ^^^^ RET505
65 |             c = 3
66 |             return x
   |
   = help: Remove unnecessary `else`

ℹ Safe fix
61 61 |         if y:  # [no-else-return]
62 62 |             b = 2
63 63 |             return y
64    |-        else:
65    |-            c = 3
66    |-            return x
   64 |+        c = 3
   65 |+        return x
67 66 |     else:
68 67 |         d = 4
69 68 |         return z

RET505.py:79:5: RET505 [*] Unnecessary `else` after `return` statement
   |
77 |             b = 2
78 |         return
79 |     else:
   |     ^^^^ RET505
80 |         c = 3
81 |     return
   |
   = help: Remove unnecessary `else`

ℹ Safe fix
76 76 |         else:
77 77 |             b = 2
78 78 |         return
79    |-    else:
80    |-        c = 3
   79 |+    c = 3
81 80 |     return
82 81 | 
83 82 | 

RET505.py:89:9: RET505 [*] Unnecessary `else` after `return` statement
   |
87 |             a = 4
88 |             return
89 |         else:
   |         ^^^^ RET505
90 |             b = 2
91 |     else:
   |
   = help: Remove unnecessary `else`

ℹ Safe fix
86 86 |         if y:  # [no-else-return]
87 87 |             a = 4
88 88 |             return
89    |-        else:
90    |-            b = 2
   89 |+        b = 2
91 90 |     else:
92 91 |         c = 3
93 92 |     return

RET505.py:99:5: RET505 [*] Unnecessary `else` after `return` statement
    |
 97 |     if x:  # [no-else-return]
 98 |         return True
 99 |     else:
    |     ^^^^ RET505
100 |         try:
101 |             return False
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
96  96  | def bar4(x):
97  97  |     if x:  # [no-else-return]
98  98  |         return True
99      |-    else:
100     |-        try:
101     |-            return False
102     |-        except ValueError:
103     |-            return None
    99  |+    try:
    100 |+        return False
    101 |+    except ValueError:
    102 |+        return None
104 103 | 
105 104 | 
106 105 | def fibo(n):

RET505.py:109:5: RET505 [*] Unnecessary `else` after `return` statement
    |
107 |     if n<2:
108 |         return n;
109 |     else:
    |     ^^^^ RET505
110 |         last = 1;
111 |         last2 = 0;
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
106 106 | def fibo(n):
107 107 |     if n<2:
108 108 |         return n;
109     |-    else:
110     |-        last = 1;
111     |-        last2 = 0;
    109 |+    last = 1;
    110 |+    last2 = 0;
112 111 | 
113 112 | 
114 113 | ###

RET505.py:145:5: RET505 [*] Unnecessary `else` after `return` statement
    |
143 |     if True:
144 |         return
145 |     else:
    |     ^^^^ RET505
146 |         # comment
147 |         pass
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
142 142 | def bar4(x):
143 143 |     if True:
144 144 |         return
145     |-    else:
146     |-        # comment
147     |-        pass
    145 |+    # comment
    146 |+    pass
148 147 | 
149 148 | 
150 149 | def bar5():

RET505.py:153:5: RET505 [*] Unnecessary `else` after `return` statement
    |
151 |     if True:
152 |         return
153 |     else:  # comment
    |     ^^^^ RET505
154 |         pass
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
150 150 | def bar5():
151 151 |     if True:
152 152 |         return
153     |-    else:  # comment
154     |-        pass
    153 |+    # comment
    154 |+    pass
155 155 | 
156 156 | 
157 157 | def bar6():

RET505.py:160:5: RET505 [*] Unnecessary `else` after `return` statement
    |
158 |     if True:
159 |         return
160 |     else\
    |     ^^^^ RET505
161 |         :\
162 |         # comment
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
157 157 | def bar6():
158 158 |     if True:
159 159 |         return
160     |-    else\
161     |-        :\
162     |-        # comment
163     |-        pass
    160 |+    # comment
    161 |+    pass
164 162 | 
165 163 | 
166 164 | def bar7():

RET505.py:169:5: RET505 [*] Unnecessary `else` after `return` statement
    |
167 |     if True:
168 |         return
169 |     else\
    |     ^^^^ RET505
170 |         :  # comment
171 |         pass
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
166 166 | def bar7():
167 167 |     if True:
168 168 |         return
169     |-    else\
170     |-        :  # comment
171     |-        pass
    169 |+    # comment
    170 |+    pass
172 171 | 
173 172 | 
174 173 | def bar8():

RET505.py:177:5: RET505 [*] Unnecessary `else` after `return` statement
    |
175 |     if True:
176 |         return
177 |     else: pass
    |     ^^^^ RET505
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
174 174 | def bar8():
175 175 |     if True:
176 176 |         return
177     |-    else: pass
    177 |+    pass
178 178 | 
179 179 | 
180 180 | def bar9():

RET505.py:183:5: RET505 [*] Unnecessary `else` after `return` statement
    |
181 |     if True:
182 |         return
183 |     else:\
    |     ^^^^ RET505
184 |         pass
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
180 180 | def bar9():
181 181 |     if True:
182 182 |         return
183     |-    else:\
184     |-        pass
    183 |+    pass
185 184 | 
186 185 | 
187 186 | x = 0

RET505.py:200:5: RET505 [*] Unnecessary `else` after `return` statement
    |
198 | def sb(self):
199 |     if self._sb is not None: return self._sb
200 |     else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
    |     ^^^^ RET505
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
197 197 | # Regression test for: https://github.com/astral-sh/ruff/issues/9732
198 198 | def sb(self):
199 199 |     if self._sb is not None: return self._sb
200     |-    else: self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
    200 |+    self._sb = '\033[01;%dm'; self._sa = '\033[0;0m';
201 201 | 
202 202 | 
203 203 | def indent(x, y, w, z):

RET505.py:207:5: RET505 [*] Unnecessary `else` after `return` statement
    |
205 |         a = 1
206 |         return y
207 |     else:
    |     ^^^^ RET505
208 | 
209 |         c = 3
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
204 204 |     if x:  # [no-else-return]
205 205 |         a = 1
206 206 |         return y
207     |-    else:
208 207 | 
209     |-        c = 3
210     |-        return z
    208 |+    c = 3
    209 |+    return z
211 210 | 
212 211 | 
213 212 | def indent(x, y, w, z):

RET505.py:217:5: RET505 [*] Unnecessary `else` after `return` statement
    |
215 |         a = 1
216 |         return y
217 |     else:
    |     ^^^^ RET505
218 |         # comment
219 |         c = 3
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
214 214 |     if x:  # [no-else-return]
215 215 |         a = 1
216 216 |         return y
217     |-    else:
218     |-        # comment
219     |-        c = 3
220     |-        return z
    217 |+    # comment
    218 |+    c = 3
    219 |+    return z
221 220 | 
222 221 | 
223 222 | def indent(x, y, w, z):

RET505.py:227:5: RET505 [*] Unnecessary `else` after `return` statement
    |
225 |         a = 1
226 |         return y
227 |     else:
    |     ^^^^ RET505
228 |           # comment
229 |         c = 3
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
224 224 |     if x:  # [no-else-return]
225 225 |         a = 1
226 226 |         return y
227     |-    else:
228     |-          # comment
229     |-        c = 3
230     |-        return z
    227 |+      # comment
    228 |+    c = 3
    229 |+    return z
231 230 | 
232 231 | 
233 232 | def indent(x, y, w, z):

RET505.py:237:5: RET505 [*] Unnecessary `else` after `return` statement
    |
235 |         a = 1
236 |         return y
237 |     else:
    |     ^^^^ RET505
238 |   # comment
239 |         c = 3
    |
    = help: Remove unnecessary `else`

ℹ Safe fix
234 234 |     if x:  # [no-else-return]
235 235 |         a = 1
236 236 |         return y
237     |-    else:
238 237 |   # comment
239     |-        c = 3
240     |-        return z
    238 |+    c = 3
    239 |+    return z


