|
1 |
| -use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then}; |
| 1 | +use clippy_utils::diagnostics::span_lint_hir_and_then; |
2 | 2 | use clippy_utils::source::{snippet_opt, snippet_with_context};
|
3 | 3 | use clippy_utils::{fn_def_id, path_to_local_id};
|
4 | 4 | use if_chain::if_chain;
|
@@ -94,9 +94,10 @@ impl<'tcx> LateLintPass<'tcx> for Return {
|
94 | 94 | if !in_external_macro(cx.sess(), retexpr.span);
|
95 | 95 | if !local.span.from_expansion();
|
96 | 96 | then {
|
97 |
| - span_lint_and_then( |
| 97 | + span_lint_hir_and_then( |
98 | 98 | cx,
|
99 | 99 | LET_AND_RETURN,
|
| 100 | + retexpr.hir_id, |
100 | 101 | retexpr.span,
|
101 | 102 | "returning the result of a `let` binding from a block",
|
102 | 103 | |err| {
|
@@ -185,6 +186,7 @@ fn check_final_expr<'tcx>(
|
185 | 186 | if !borrows {
|
186 | 187 | emit_return_lint(
|
187 | 188 | cx,
|
| 189 | + inner.map_or(expr.hir_id, |inner| inner.hir_id), |
188 | 190 | span.expect("`else return` is not possible"),
|
189 | 191 | inner.as_ref().map(|i| i.span),
|
190 | 192 | replacement,
|
@@ -220,50 +222,81 @@ fn check_final_expr<'tcx>(
|
220 | 222 | }
|
221 | 223 | }
|
222 | 224 |
|
223 |
| -fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) { |
| 225 | +fn emit_return_lint( |
| 226 | + cx: &LateContext<'_>, |
| 227 | + emission_place: HirId, |
| 228 | + ret_span: Span, |
| 229 | + inner_span: Option<Span>, |
| 230 | + replacement: RetReplacement, |
| 231 | +) { |
224 | 232 | if ret_span.from_expansion() {
|
225 | 233 | return;
|
226 | 234 | }
|
227 | 235 | match inner_span {
|
228 | 236 | Some(inner_span) => {
|
229 | 237 | let mut applicability = Applicability::MachineApplicable;
|
230 |
| - span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| { |
231 |
| - let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability); |
232 |
| - diag.span_suggestion(ret_span, "remove `return`", snippet, applicability); |
233 |
| - }); |
| 238 | + span_lint_hir_and_then( |
| 239 | + cx, |
| 240 | + NEEDLESS_RETURN, |
| 241 | + emission_place, |
| 242 | + ret_span, |
| 243 | + "unneeded `return` statement", |
| 244 | + |diag| { |
| 245 | + let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability); |
| 246 | + diag.span_suggestion(ret_span, "remove `return`", snippet, applicability); |
| 247 | + }, |
| 248 | + ); |
234 | 249 | },
|
235 | 250 | None => match replacement {
|
236 | 251 | RetReplacement::Empty => {
|
237 |
| - span_lint_and_sugg( |
| 252 | + span_lint_hir_and_then( |
238 | 253 | cx,
|
239 | 254 | NEEDLESS_RETURN,
|
| 255 | + emission_place, |
240 | 256 | ret_span,
|
241 | 257 | "unneeded `return` statement",
|
242 |
| - "remove `return`", |
243 |
| - String::new(), |
244 |
| - Applicability::MachineApplicable, |
| 258 | + |diag| { |
| 259 | + diag.span_suggestion( |
| 260 | + ret_span, |
| 261 | + "remove `return`", |
| 262 | + String::new(), |
| 263 | + Applicability::MachineApplicable, |
| 264 | + ); |
| 265 | + }, |
245 | 266 | );
|
246 | 267 | },
|
247 | 268 | RetReplacement::Block => {
|
248 |
| - span_lint_and_sugg( |
| 269 | + span_lint_hir_and_then( |
249 | 270 | cx,
|
250 | 271 | NEEDLESS_RETURN,
|
| 272 | + emission_place, |
251 | 273 | ret_span,
|
252 | 274 | "unneeded `return` statement",
|
253 |
| - "replace `return` with an empty block", |
254 |
| - "{}".to_string(), |
255 |
| - Applicability::MachineApplicable, |
| 275 | + |diag| { |
| 276 | + diag.span_suggestion( |
| 277 | + ret_span, |
| 278 | + "replace `return` with an empty block", |
| 279 | + "{}".to_string(), |
| 280 | + Applicability::MachineApplicable, |
| 281 | + ); |
| 282 | + }, |
256 | 283 | );
|
257 | 284 | },
|
258 | 285 | RetReplacement::Unit => {
|
259 |
| - span_lint_and_sugg( |
| 286 | + span_lint_hir_and_then( |
260 | 287 | cx,
|
261 | 288 | NEEDLESS_RETURN,
|
| 289 | + emission_place, |
262 | 290 | ret_span,
|
263 | 291 | "unneeded `return` statement",
|
264 |
| - "replace `return` with a unit value", |
265 |
| - "()".to_string(), |
266 |
| - Applicability::MachineApplicable, |
| 292 | + |diag| { |
| 293 | + diag.span_suggestion( |
| 294 | + ret_span, |
| 295 | + "replace `return` with a unit value", |
| 296 | + "()".to_string(), |
| 297 | + Applicability::MachineApplicable, |
| 298 | + ); |
| 299 | + }, |
267 | 300 | );
|
268 | 301 | },
|
269 | 302 | },
|
|
0 commit comments