Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overflowing literal lint in loops #61098

Merged
merged 3 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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