Skip to content

Commit

Permalink
Rollup merge of #61098 - varkor:fix-overflowing-literal-in-loop, r=es…
Browse files Browse the repository at this point in the history
…tebank

Fix overflowing literal lint in loops

Fixes #60459.

r? @estebank
  • Loading branch information
Centril authored May 23, 2019
2 parents 3388028 + 12de24c commit 92fda92
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
31 changes: 16 additions & 15 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::util::nodemap::NodeMap;
use errors::{DiagnosticBuilder, DiagnosticId};
use std::{hash, ptr};
use syntax::ast;
use syntax::source_map::{MultiSpan, ExpnFormat};
use syntax::source_map::{MultiSpan, ExpnFormat, CompilerDesugaringKind};
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::edition::Edition;
use syntax::symbol::{Symbol, sym};
Expand Down Expand Up @@ -887,21 +887,22 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
};

match info.format {
ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
ExpnFormat::MacroBang(..) => {} // check below
}

let def_site = match info.def_site {
Some(span) => span,
// no span for the def_site means it's an external macro
None => return true,
};
ExpnFormat::MacroAttribute(..) => true, // definitely a plugin
ExpnFormat::CompilerDesugaring(CompilerDesugaringKind::ForLoop) => false,
ExpnFormat::CompilerDesugaring(_) => true, // well, it's "external"
ExpnFormat::MacroBang(..) => {
let def_site = match info.def_site {
Some(span) => span,
// no span for the def_site means it's an external macro
None => return true,
};

match sess.source_map().span_to_snippet(def_site) {
Ok(code) => !code.starts_with("macro_rules"),
// no snippet = external macro or compiler-builtin expansion
Err(_) => true,
match sess.source_map().span_to_snippet(def_site) {
Ok(code) => !code.starts_with("macro_rules"),
// no snippet = external macro or compiler-builtin expansion
Err(_) => true,
}
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/lint/deny-overflowing-literals.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
fn main() {
let x: u8 = 256;
//~^ error: literal out of range for `u8`

for _ in 0..256u8 {}
//~^ error: range endpoint is out of range for `u8`
}
8 changes: 7 additions & 1 deletion src/test/ui/lint/deny-overflowing-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@ LL | let x: u8 = 256;
|
= note: #[deny(overflowing_literals)] on by default

error: aborting due to previous error
error: range endpoint is out of range for `u8`
--> $DIR/deny-overflowing-literals.rs:5:14
|
LL | for _ in 0..256u8 {}
| ^^^^^^^^ help: use an inclusive range instead: `0..=255u8`

error: aborting due to 2 previous errors

1 change: 1 addition & 0 deletions src/test/ui/unreachable/unreachable-loop-patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ impl Iterator for Void {
fn main() {
for _ in unimplemented!() as Void {}
//~^ ERROR unreachable pattern
//~^^ ERROR unreachable pattern
}
8 changes: 7 additions & 1 deletion src/test/ui/unreachable/unreachable-loop-patterns.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ note: lint level defined here
LL | #![deny(unreachable_patterns)]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error
error: unreachable pattern
--> $DIR/unreachable-loop-patterns.rs:20:14
|
LL | for _ in unimplemented!() as Void {}
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

0 comments on commit 92fda92

Please sign in to comment.