Skip to content

Commit cc4bc57

Browse files
committed
Fix clippy with changed macro statement spans
1 parent 90f4521 commit cc4bc57

30 files changed

+126
-130
lines changed

src/tools/clippy/clippy_lints/src/copies.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::{Applicability, DiagnosticBuilder};
1010
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
1111
use rustc_hir::{Block, Expr, ExprKind, HirId};
12-
use rustc_lint::{LateContext, LateLintPass};
12+
use rustc_lint::{LateContext, LateLintPass, LintContext};
1313
use rustc_middle::hir::map::Map;
1414
use rustc_session::{declare_lint_pass, declare_tool_lint};
1515
use rustc_span::{source_map::Span, symbol::Symbol, BytePos};
@@ -432,10 +432,11 @@ fn emit_branches_sharing_code_lint(
432432
let mut add_expr_note = false;
433433

434434
// Construct suggestions
435+
let sm = cx.sess().source_map();
435436
if start_stmts > 0 {
436437
let block = blocks[0];
437438
let span_start = first_line_of_span(cx, if_expr.span).shrink_to_lo();
438-
let span_end = block.stmts[start_stmts - 1].span.source_callsite();
439+
let span_end = sm.stmt_span(block.stmts[start_stmts - 1].span, block.span);
439440

440441
let cond_span = first_line_of_span(cx, if_expr.span).until(block.span);
441442
let cond_snippet = reindent_multiline(snippet(cx, cond_span, "_"), false, None);
@@ -454,15 +455,16 @@ fn emit_branches_sharing_code_lint(
454455
let span_end = block.span.shrink_to_hi();
455456

456457
let moved_start = if end_stmts == 0 && block.expr.is_some() {
457-
block.expr.unwrap().span
458+
block.expr.unwrap().span.source_callsite()
458459
} else {
459-
block.stmts[block.stmts.len() - end_stmts].span
460-
}
461-
.source_callsite();
460+
sm.stmt_span(block.stmts[block.stmts.len() - end_stmts].span, block.span)
461+
};
462462
let moved_end = block
463463
.expr
464-
.map_or_else(|| block.stmts[block.stmts.len() - 1].span, |expr| expr.span)
465-
.source_callsite();
464+
.map_or_else(
465+
|| sm.stmt_span(block.stmts[block.stmts.len() - 1].span, block.span),
466+
|expr| expr.span.source_callsite(),
467+
);
466468

467469
let moved_span = moved_start.to(moved_end);
468470
let moved_snipped = reindent_multiline(snippet(cx, moved_span, "_"), true, None);

src/tools/clippy/clippy_lints/src/format.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessFormat {
9090
}
9191
}
9292

93-
fn span_useless_format(cx: &LateContext<'_>, span: Span, mut sugg: String, mut applicability: Applicability) {
94-
// The callsite span contains the statement semicolon for some reason.
95-
if snippet_with_applicability(cx, span, "..", &mut applicability).ends_with(';') {
96-
sugg.push(';');
97-
}
98-
93+
fn span_useless_format(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
9994
span_lint_and_sugg(
10095
cx,
10196
USELESS_FORMAT,

src/tools/clippy/clippy_lints/src/needless_continue.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@
3636
use clippy_utils::diagnostics::span_lint_and_help;
3737
use clippy_utils::source::{indent_of, snippet, snippet_block};
3838
use rustc_ast::ast;
39-
use rustc_lint::{EarlyContext, EarlyLintPass};
39+
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
4040
use rustc_session::{declare_lint_pass, declare_tool_lint};
41-
use rustc_span::source_map::{original_sp, DUMMY_SP};
4241
use rustc_span::Span;
4342

4443
declare_clippy_lint! {
@@ -270,7 +269,7 @@ struct LintData<'a> {
270269
/// The 0-based index of the `if` statement in the containing loop block.
271270
stmt_idx: usize,
272271
/// The statements of the loop block.
273-
block_stmts: &'a [ast::Stmt],
272+
loop_block: &'a ast::Block,
274273
}
275274

276275
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:
343342
let indent = span_of_first_expr_in_block(data.if_block)
344343
.and_then(|span| indent_of(cx, span))
345344
.unwrap_or(0);
346-
let to_annex = data.block_stmts[data.stmt_idx + 1..]
345+
let to_annex = data.loop_block.stmts[data.stmt_idx + 1..]
347346
.iter()
348-
.map(|stmt| original_sp(stmt.span, DUMMY_SP))
349-
.map(|span| {
347+
.map(|stmt| {
348+
let span = cx.sess().source_map().stmt_span(stmt.span, data.loop_block.span);
350349
let snip = snippet_block(cx, span, "..", None).into_owned();
351350
snip.lines()
352351
.map(|line| format!("{}{}", " ".repeat(indent), line))
@@ -393,7 +392,7 @@ fn check_and_warn<'a>(cx: &EarlyContext<'_>, expr: &'a ast::Expr) {
393392
if_cond: cond,
394393
if_block: then_block,
395394
else_expr,
396-
block_stmts: &loop_block.stmts,
395+
loop_block,
397396
};
398397
if needless_continue_in_else(else_expr, label) {
399398
emit_warning(

src/tools/clippy/tests/ui-toml/nonstandard_macro_braces/conf_nonstandard_macro_braces.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ error: use of irregular braces for `eprint!` macro
8282
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
8383
|
8484
LL | eprint!("test if user config overrides defaults");
85-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8686
|
87-
help: consider writing `eprint!["test if user config overrides defaults"];`
87+
help: consider writing `eprint!["test if user config overrides defaults"]`
8888
--> $DIR/conf_nonstandard_macro_braces.rs:57:5
8989
|
9090
LL | eprint!("test if user config overrides defaults");
91-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9292

9393
error: aborting due to 7 previous errors
9494

src/tools/clippy/tests/ui/asm_syntax.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: Intel x86 assembly syntax used
22
--> $DIR/asm_syntax.rs:9:9
33
|
44
LL | asm!("");
5-
| ^^^^^^^^^
5+
| ^^^^^^^^
66
|
77
= note: `-D clippy::inline-asm-x86-intel-syntax` implied by `-D warnings`
88
= help: use AT&T x86 assembly syntax
@@ -11,23 +11,23 @@ error: Intel x86 assembly syntax used
1111
--> $DIR/asm_syntax.rs:10:9
1212
|
1313
LL | asm!("", options());
14-
| ^^^^^^^^^^^^^^^^^^^^
14+
| ^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: use AT&T x86 assembly syntax
1717

1818
error: Intel x86 assembly syntax used
1919
--> $DIR/asm_syntax.rs:11:9
2020
|
2121
LL | asm!("", options(nostack));
22-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: use AT&T x86 assembly syntax
2525

2626
error: AT&T x86 assembly syntax used
2727
--> $DIR/asm_syntax.rs:23:9
2828
|
2929
LL | asm!("", options(att_syntax));
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3131
|
3232
= note: `-D clippy::inline-asm-x86-att-syntax` implied by `-D warnings`
3333
= help: use Intel x86 assembly syntax
@@ -36,7 +36,7 @@ error: AT&T x86 assembly syntax used
3636
--> $DIR/asm_syntax.rs:24:9
3737
|
3838
LL | asm!("", options(nostack, att_syntax));
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= help: use Intel x86 assembly syntax
4242

src/tools/clippy/tests/ui/assertions_on_constants.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: `assert!(true)` will be optimized out by the compiler
22
--> $DIR/assertions_on_constants.rs:11:5
33
|
44
LL | assert!(true);
5-
| ^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::assertions-on-constants` implied by `-D warnings`
88
= help: remove it
@@ -12,7 +12,7 @@ error: `assert!(false)` should probably be replaced
1212
--> $DIR/assertions_on_constants.rs:12:5
1313
|
1414
LL | assert!(false);
15-
| ^^^^^^^^^^^^^^^
15+
| ^^^^^^^^^^^^^^
1616
|
1717
= help: use `panic!()` or `unreachable!()`
1818
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -21,7 +21,7 @@ error: `assert!(true)` will be optimized out by the compiler
2121
--> $DIR/assertions_on_constants.rs:13:5
2222
|
2323
LL | assert!(true, "true message");
24-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2525
|
2626
= help: remove it
2727
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -30,7 +30,7 @@ error: `assert!(false, "false message")` should probably be replaced
3030
--> $DIR/assertions_on_constants.rs:14:5
3131
|
3232
LL | assert!(false, "false message");
33-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3434
|
3535
= help: use `panic!("false message")` or `unreachable!("false message")`
3636
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -39,7 +39,7 @@ error: `assert!(false, msg.to_uppercase())` should probably be replaced
3939
--> $DIR/assertions_on_constants.rs:17:5
4040
|
4141
LL | assert!(false, msg.to_uppercase());
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4343
|
4444
= help: use `panic!(msg.to_uppercase())` or `unreachable!(msg.to_uppercase())`
4545
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -48,7 +48,7 @@ error: `assert!(true)` will be optimized out by the compiler
4848
--> $DIR/assertions_on_constants.rs:20:5
4949
|
5050
LL | assert!(B);
51-
| ^^^^^^^^^^^
51+
| ^^^^^^^^^^
5252
|
5353
= help: remove it
5454
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -57,7 +57,7 @@ error: `assert!(false)` should probably be replaced
5757
--> $DIR/assertions_on_constants.rs:23:5
5858
|
5959
LL | assert!(C);
60-
| ^^^^^^^^^^^
60+
| ^^^^^^^^^^
6161
|
6262
= help: use `panic!()` or `unreachable!()`
6363
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -66,7 +66,7 @@ error: `assert!(false, "C message")` should probably be replaced
6666
--> $DIR/assertions_on_constants.rs:24:5
6767
|
6868
LL | assert!(C, "C message");
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^
69+
| ^^^^^^^^^^^^^^^^^^^^^^^
7070
|
7171
= help: use `panic!("C message")` or `unreachable!("C message")`
7272
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -75,7 +75,7 @@ error: `debug_assert!(true)` will be optimized out by the compiler
7575
--> $DIR/assertions_on_constants.rs:26:5
7676
|
7777
LL | debug_assert!(true);
78-
| ^^^^^^^^^^^^^^^^^^^^
78+
| ^^^^^^^^^^^^^^^^^^^
7979
|
8080
= help: remove it
8181
= note: this error originates in the macro `$crate::assert` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)