Skip to content

Commit

Permalink
Fix clippy with changed macro statement spans
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed Oct 15, 2021
1 parent 90f4521 commit cc4bc57
Show file tree
Hide file tree
Showing 30 changed files with 126 additions and 130 deletions.
18 changes: 10 additions & 8 deletions src/tools/clippy/clippy_lints/src/copies.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
7 changes: 1 addition & 6 deletions src/tools/clippy/clippy_lints/src/format.rs
Expand Up @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions src/tools/clippy/clippy_lints/src/needless_continue.rs
Expand Up @@ -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! {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -82,13 +82,13 @@ error: use of irregular braces for `eprint!` macro
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
|
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider writing `eprint!["test if user config overrides defaults"];`
help: consider writing `eprint!["test if user config overrides defaults"]`
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
|
LL | eprint!("test if user config overrides defaults");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors

10 changes: 5 additions & 5 deletions src/tools/clippy/tests/ui/asm_syntax.stderr
Expand Up @@ -2,7 +2,7 @@ error: Intel x86 assembly syntax used
--> $DIR/asm_syntax.rs:9:9
|
LL | asm!("");
| ^^^^^^^^^
| ^^^^^^^^
|
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
= help: use AT&T x86 assembly syntax
Expand All @@ -11,23 +11,23 @@ error: Intel x86 assembly syntax used
--> $DIR/asm_syntax.rs:10:9
|
LL | asm!("", options());
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^
|
= help: use AT&T x86 assembly syntax

error: Intel x86 assembly syntax used
--> $DIR/asm_syntax.rs:11:9
|
LL | asm!("", options(nostack));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use AT&T x86 assembly syntax

error: AT&T x86 assembly syntax used
--> $DIR/asm_syntax.rs:23:9
|
LL | asm!("", options(att_syntax));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
= help: use Intel x86 assembly syntax
Expand All @@ -36,7 +36,7 @@ error: AT&T x86 assembly syntax used
--> $DIR/asm_syntax.rs:24:9
|
LL | asm!("", options(nostack, att_syntax));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use Intel x86 assembly syntax

Expand Down
18 changes: 9 additions & 9 deletions src/tools/clippy/tests/ui/assertions_on_constants.stderr
Expand Up @@ -2,7 +2,7 @@ error: `assert!(true)` will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:11:5
|
LL | assert!(true);
| ^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^
|
= note: `-D clippy::assertions-on-constants` implied by `-D warnings`
= help: remove it
Expand All @@ -12,7 +12,7 @@ error: `assert!(false)` should probably be replaced
--> $DIR/assertions_on_constants.rs:12:5
|
LL | assert!(false);
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= help: use `panic!()` or `unreachable!()`
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -21,7 +21,7 @@ error: `assert!(true)` will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:13:5
|
LL | assert!(true, "true message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: remove it
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -30,7 +30,7 @@ error: `assert!(false, "false message")` should probably be replaced
--> $DIR/assertions_on_constants.rs:14:5
|
LL | assert!(false, "false message");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `panic!("false message")` or `unreachable!("false message")`
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -39,7 +39,7 @@ error: `assert!(false, msg.to_uppercase())` should probably be replaced
--> $DIR/assertions_on_constants.rs:17:5
|
LL | assert!(false, msg.to_uppercase());
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `panic!(msg.to_uppercase())` or `unreachable!(msg.to_uppercase())`
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -48,7 +48,7 @@ error: `assert!(true)` will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:20:5
|
LL | assert!(B);
| ^^^^^^^^^^^
| ^^^^^^^^^^
|
= help: remove it
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -57,7 +57,7 @@ error: `assert!(false)` should probably be replaced
--> $DIR/assertions_on_constants.rs:23:5
|
LL | assert!(C);
| ^^^^^^^^^^^
| ^^^^^^^^^^
|
= help: use `panic!()` or `unreachable!()`
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -66,7 +66,7 @@ error: `assert!(false, "C message")` should probably be replaced
--> $DIR/assertions_on_constants.rs:24:5
|
LL | assert!(C, "C message");
| ^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^^^
|
= help: use `panic!("C message")` or `unreachable!("C message")`
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -75,7 +75,7 @@ error: `debug_assert!(true)` will be optimized out by the compiler
--> $DIR/assertions_on_constants.rs:26:5
|
LL | debug_assert!(true);
| ^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^
|
= help: remove it
= note: this error originates in the macro `$crate::assert` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand Down

0 comments on commit cc4bc57

Please sign in to comment.