Skip to content

Commit 11e9c48

Browse files
committed
Flag unsafe blocks from format! as compiler-generated
1 parent 19a6fab commit 11e9c48

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

src/librustc/middle/lint.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,9 @@ impl Visitor<@mut Context> for UnusedUnsafeLintVisitor {
11331133
match e.node {
11341134
// Don't warn about generated blocks, that'll just pollute the
11351135
// output.
1136-
ast::ExprBlock(ref blk) if blk.rules == ast::UnsafeBlock(false) => {
1137-
if !cx.tcx.used_unsafe.contains(&blk.id) {
1136+
ast::ExprBlock(ref blk) => {
1137+
if blk.rules == ast::UnsafeBlock(ast::UserProvided) &&
1138+
!cx.tcx.used_unsafe.contains(&blk.id) {
11381139
cx.span_lint(unused_unsafe, blk.span,
11391140
"unnecessary `unsafe` block");
11401141
}

src/libsyntax/ast.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,13 @@ pub struct Field {
479479
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
480480
pub enum BlockCheckMode {
481481
DefaultBlock,
482-
UnsafeBlock(/* generated internally */ bool),
482+
UnsafeBlock(UnsafeSource),
483+
}
484+
485+
#[deriving(Clone, Eq, Encodable, Decodable, IterBytes)]
486+
pub enum UnsafeSource {
487+
CompilerGenerated,
488+
UserProvided,
483489
}
484490

485491
#[deriving(Clone, Eq, Encodable, Decodable,IterBytes)]

src/libsyntax/ext/ifmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ impl Context {
632632
stmts: ~[],
633633
expr: Some(result),
634634
id: ast::DUMMY_NODE_ID,
635-
rules: ast::UnsafeBlock,
635+
rules: ast::UnsafeBlock(ast::CompilerGenerated),
636636
span: self.fmtsp,
637637
});
638638

src/libsyntax/parse/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,7 @@ impl Parser {
17921792
} else if self.eat_keyword(keywords::Match) {
17931793
return self.parse_match_expr();
17941794
} else if self.eat_keyword(keywords::Unsafe) {
1795-
return self.parse_block_expr(lo, UnsafeBlock(false));
1795+
return self.parse_block_expr(lo, UnsafeBlock(ast::UserProvided));
17961796
} else if *self.token == token::LBRACKET {
17971797
self.bump();
17981798
let mutbl = self.parse_mutability();

src/test/run-pass/ifmt.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
// xfail-fast: check-fast screws up repr paths
1212

13+
#[deny(warnings)];
14+
1315
use std::fmt;
1416

1517
struct A;
@@ -226,6 +228,13 @@ pub fn main() {
226228
let a = ~3;
227229
format!("{:?}", a);
228230
format!("{:?}", a);
231+
232+
// make sure that format! doesn't cause spurious unused-unsafe warnings when
233+
// it's inside of an outer unsafe block
234+
unsafe {
235+
let a: int = ::std::cast::transmute(3u);
236+
format!("{}", a);
237+
}
229238
}
230239

231240
// Basic test to make sure that we can invoke the `write!` macro with an

0 commit comments

Comments
 (0)