Fix clippy with changed macro statement spans

This commit is contained in:
Cameron Steffen
2021-10-14 13:28:30 -05:00
parent 049ab82662
commit 0a23fff82d
30 changed files with 126 additions and 130 deletions

View File

@@ -9,7 +9,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::{Block, Expr, ExprKind, HirId};
use rustc_lint::{LateContext, LateLintPass};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::map::Map;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{source_map::Span, symbol::Symbol, BytePos};
@@ -432,10 +432,11 @@ fn emit_branches_sharing_code_lint(
let mut add_expr_note = false;
// Construct suggestions
let sm = cx.sess().source_map();
if start_stmts > 0 {
let block = blocks[0];
let span_start = first_line_of_span(cx, if_expr.span).shrink_to_lo();
let span_end = block.stmts[start_stmts - 1].span.source_callsite();
let span_end = sm.stmt_span(block.stmts[start_stmts - 1].span, block.span);
let cond_span = first_line_of_span(cx, if_expr.span).until(block.span);
let cond_snippet = reindent_multiline(snippet(cx, cond_span, "_"), false, None);
@@ -454,15 +455,16 @@ fn emit_branches_sharing_code_lint(
let span_end = block.span.shrink_to_hi();
let moved_start = if end_stmts == 0 && block.expr.is_some() {
block.expr.unwrap().span
block.expr.unwrap().span.source_callsite()
} else {
block.stmts[block.stmts.len() - end_stmts].span
}
.source_callsite();
sm.stmt_span(block.stmts[block.stmts.len() - end_stmts].span, block.span)
};
let moved_end = block
.expr
.map_or_else(|| block.stmts[block.stmts.len() - 1].span, |expr| expr.span)
.source_callsite();
.map_or_else(
|| sm.stmt_span(block.stmts[block.stmts.len() - 1].span, block.span),
|expr| expr.span.source_callsite(),
);
let moved_span = moved_start.to(moved_end);
let moved_snipped = reindent_multiline(snippet(cx, moved_span, "_"), true, None);

View File

@@ -90,12 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
}
}
fn span_useless_format(cx: &LateContext<'_>, span: Span, mut sugg: String, mut applicability: Applicability) {
// The callsite span contains the statement semicolon for some reason.
if snippet_with_applicability(cx, span, "..", &mut applicability).ends_with(';') {
sugg.push(';');
}
fn span_useless_format(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
span_lint_and_sugg(
cx,
USELESS_FORMAT,

View File

@@ -36,9 +36,8 @@
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::source::{indent_of, snippet, snippet_block};
use rustc_ast::ast;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::{original_sp, DUMMY_SP};
use rustc_span::Span;
declare_clippy_lint! {
@@ -270,7 +269,7 @@ struct LintData<'a> {
/// The 0-based index of the `if` statement in the containing loop block.
stmt_idx: usize,
/// The statements of the loop block.
block_stmts: &'a [ast::Stmt],
loop_block: &'a ast::Block,
}
const MSG_REDUNDANT_CONTINUE_EXPRESSION: &str = "this `continue` expression is redundant";
@@ -343,10 +342,10 @@ fn suggestion_snippet_for_continue_inside_else<'a>(cx: &EarlyContext<'_>, data:
let indent = span_of_first_expr_in_block(data.if_block)
.and_then(|span| indent_of(cx, span))
.unwrap_or(0);
let to_annex = data.block_stmts[data.stmt_idx + 1..]
let to_annex = data.loop_block.stmts[data.stmt_idx + 1..]
.iter()
.map(|stmt| original_sp(stmt.span, DUMMY_SP))
.map(|span| {
.map(|stmt| {
let span = cx.sess().source_map().stmt_span(stmt.span, data.loop_block.span);
let snip = snippet_block(cx, span, "..", None).into_owned();
snip.lines()
.map(|line| format!("{}{}", " ".repeat(indent), line))
@@ -393,7 +392,7 @@ fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
if_cond: cond,
if_block: then_block,
else_expr,
block_stmts: &loop_block.stmts,
loop_block,
};
if needless_continue_in_else(else_expr, label) {
emit_warning(