use rustc_middle::ty::subst::GenericArgKind;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
+use rustc_span::{BytePos, Pos};
declare_clippy_lint! {
/// ### What it does
if cx.tcx.hir().attrs(expr.hir_id).is_empty() {
let borrows = inner.map_or(false, |inner| last_statement_borrows(cx, inner));
if !borrows {
- emit_return_lint(
- cx,
- peeled_drop_expr.span,
- semi_spans,
- inner.as_ref().map(|i| i.span),
- replacement,
- );
+ // check if expr return nothing
+ let ret_span = if inner.is_none() && replacement == RetReplacement::Empty {
+ extend_span_to_previous_non_ws(cx, peeled_drop_expr.span)
+ } else {
+ peeled_drop_expr.span
+ };
+
+ emit_return_lint(cx, ret_span, semi_spans, inner.as_ref().map(|i| i.span), replacement);
}
}
},
})
.is_some()
}
+
+// Go backwards while encountering whitespace and extend the given Span to that point.
+fn extend_span_to_previous_non_ws(cx: &LateContext<'_>, sp: Span) -> Span {
+ if let Ok(prev_source) = cx.sess().source_map().span_to_prev_source(sp) {
+ let ws = [' ', '\t', '\n'];
+ if let Some(non_ws_pos) = prev_source.rfind(|c| !ws.contains(&c)) {
+ let len = prev_source.len() - non_ws_pos - 1;
+ return sp.with_lo(sp.lo() - BytePos::from_usize(len));
+ }
+ }
+
+ sp
+}