Skip to content

Commit fc54bf9

Browse files
committed
Auto merge of #44195 - alexcrichton:remove-used-unsafe, r=nikomatsakis
rustc: Remove the `used_unsafe` field on TyCtxt Now that lint levels are available for the entire compilation, this can be an entirely local lint in `effect.rs` cc #44137
2 parents 5e9431a + 058202e commit fc54bf9

File tree

6 files changed

+66
-74
lines changed

6 files changed

+66
-74
lines changed

src/librustc/lint/builtin.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ declare_lint! {
216216
"detects use of deprecated items"
217217
}
218218

219+
declare_lint! {
220+
pub UNUSED_UNSAFE,
221+
Warn,
222+
"unnecessary use of an `unsafe` block"
223+
}
224+
219225
/// Does nothing as a lint pass, but registers some `Lint`s
220226
/// which are used by other parts of the compiler.
221227
#[derive(Copy, Clone)]
@@ -256,7 +262,8 @@ impl LintPass for HardwiredLints {
256262
MISSING_FRAGMENT_SPECIFIER,
257263
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
258264
LATE_BOUND_LIFETIME_ARGUMENTS,
259-
DEPRECATED
265+
DEPRECATED,
266+
UNUSED_UNSAFE
260267
)
261268
}
262269
}

src/librustc/middle/effect.rs

+50-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ use self::RootUnsafeContext::*;
1414

1515
use ty::{self, TyCtxt};
1616
use lint;
17+
use lint::builtin::UNUSED_UNSAFE;
1718

18-
use syntax::ast;
19-
use syntax_pos::Span;
20-
use hir::{self, PatKind};
2119
use hir::def::Def;
2220
use hir::intravisit::{self, FnKind, Visitor, NestedVisitorMap};
21+
use hir::{self, PatKind};
22+
use syntax::ast;
23+
use syntax_pos::Span;
24+
use util::nodemap::FxHashSet;
2325

2426
#[derive(Copy, Clone)]
2527
struct UnsafeContext {
@@ -47,6 +49,7 @@ struct EffectCheckVisitor<'a, 'tcx: 'a> {
4749

4850
/// Whether we're in an unsafe context.
4951
unsafe_context: UnsafeContext,
52+
used_unsafe: FxHashSet<ast::NodeId>,
5053
}
5154

5255
impl<'a, 'tcx> EffectCheckVisitor<'a, 'tcx> {
@@ -73,7 +76,7 @@ impl<'a, 'tcx> EffectCheckVisitor<'a, 'tcx> {
7376
UnsafeBlock(block_id) => {
7477
// OK, but record this.
7578
debug!("effect: recording unsafe block as used: {}", block_id);
76-
self.tcx.used_unsafe.borrow_mut().insert(block_id);
79+
self.used_unsafe.insert(block_id);
7780
}
7881
UnsafeFn => {}
7982
}
@@ -159,7 +162,48 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
159162

160163
intravisit::walk_block(self, block);
161164

162-
self.unsafe_context = old_unsafe_context
165+
self.unsafe_context = old_unsafe_context;
166+
167+
// Don't warn about generated blocks, that'll just pollute the output.
168+
match block.rules {
169+
hir::UnsafeBlock(hir::UserProvided) => {}
170+
_ => return,
171+
}
172+
if self.used_unsafe.contains(&block.id) {
173+
return
174+
}
175+
176+
/// Return the NodeId for an enclosing scope that is also `unsafe`
177+
fn is_enclosed(tcx: TyCtxt,
178+
used_unsafe: &FxHashSet<ast::NodeId>,
179+
id: ast::NodeId) -> Option<(String, ast::NodeId)> {
180+
let parent_id = tcx.hir.get_parent_node(id);
181+
if parent_id != id {
182+
if used_unsafe.contains(&parent_id) {
183+
Some(("block".to_string(), parent_id))
184+
} else if let Some(hir::map::NodeItem(&hir::Item {
185+
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
186+
..
187+
})) = tcx.hir.find(parent_id) {
188+
Some(("fn".to_string(), parent_id))
189+
} else {
190+
is_enclosed(tcx, used_unsafe, parent_id)
191+
}
192+
} else {
193+
None
194+
}
195+
}
196+
197+
let mut db = self.tcx.struct_span_lint_node(UNUSED_UNSAFE,
198+
block.id,
199+
block.span,
200+
"unnecessary `unsafe` block");
201+
db.span_label(block.span, "unnecessary `unsafe` block");
202+
if let Some((kind, id)) = is_enclosed(self.tcx, &self.used_unsafe, block.id) {
203+
db.span_note(self.tcx.hir.span(id),
204+
&format!("because it's nested under this `unsafe` {}", kind));
205+
}
206+
db.emit();
163207
}
164208

165209
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
@@ -265,6 +309,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
265309
tables: &ty::TypeckTables::empty(None),
266310
body_id: hir::BodyId { node_id: ast::CRATE_NODE_ID },
267311
unsafe_context: UnsafeContext::new(SafeContext),
312+
used_unsafe: FxHashSet(),
268313
};
269314

270315
tcx.hir.krate().visit_all_item_likes(&mut visitor.as_deep_visitor());

src/librustc/ty/context.rs

-5
Original file line numberDiff line numberDiff line change
@@ -853,10 +853,6 @@ pub struct GlobalCtxt<'tcx> {
853853

854854
pub lang_items: middle::lang_items::LanguageItems,
855855

856-
/// Set of used unsafe nodes (functions or blocks). Unsafe nodes not
857-
/// present in this set can be warned about.
858-
pub used_unsafe: RefCell<NodeSet>,
859-
860856
/// Set of nodes which mark locals as mutable which end up getting used at
861857
/// some point. Local variable definitions not in this set can be warned
862858
/// about.
@@ -1092,7 +1088,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
10921088
normalized_cache: RefCell::new(FxHashMap()),
10931089
inhabitedness_cache: RefCell::new(FxHashMap()),
10941090
lang_items,
1095-
used_unsafe: RefCell::new(NodeSet()),
10961091
used_mut_nodes: RefCell::new(NodeSet()),
10971092
stability: RefCell::new(stability),
10981093
selection_cache: traits::SelectionCache::new(),

src/librustc_lint/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
128128
NonSnakeCase,
129129
NonUpperCaseGlobals,
130130
NonShorthandFieldPatterns,
131-
UnusedUnsafe,
132131
UnsafeCode,
133132
UnusedMut,
134133
UnusedAllocation,

src/librustc_lint/unused.rs

-54
Original file line numberDiff line numberDiff line change
@@ -204,60 +204,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
204204
}
205205
}
206206

207-
declare_lint! {
208-
pub UNUSED_UNSAFE,
209-
Warn,
210-
"unnecessary use of an `unsafe` block"
211-
}
212-
213-
#[derive(Copy, Clone)]
214-
pub struct UnusedUnsafe;
215-
216-
impl LintPass for UnusedUnsafe {
217-
fn get_lints(&self) -> LintArray {
218-
lint_array!(UNUSED_UNSAFE)
219-
}
220-
}
221-
222-
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedUnsafe {
223-
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
224-
/// Return the NodeId for an enclosing scope that is also `unsafe`
225-
fn is_enclosed(cx: &LateContext, id: ast::NodeId) -> Option<(String, ast::NodeId)> {
226-
let parent_id = cx.tcx.hir.get_parent_node(id);
227-
if parent_id != id {
228-
if cx.tcx.used_unsafe.borrow().contains(&parent_id) {
229-
Some(("block".to_string(), parent_id))
230-
} else if let Some(hir::map::NodeItem(&hir::Item {
231-
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
232-
..
233-
})) = cx.tcx.hir.find(parent_id) {
234-
Some(("fn".to_string(), parent_id))
235-
} else {
236-
is_enclosed(cx, parent_id)
237-
}
238-
} else {
239-
None
240-
}
241-
}
242-
if let hir::ExprBlock(ref blk) = e.node {
243-
// Don't warn about generated blocks, that'll just pollute the output.
244-
if blk.rules == hir::UnsafeBlock(hir::UserProvided) &&
245-
!cx.tcx.used_unsafe.borrow().contains(&blk.id) {
246-
247-
let mut db = cx.struct_span_lint(UNUSED_UNSAFE, blk.span,
248-
"unnecessary `unsafe` block");
249-
250-
db.span_label(blk.span, "unnecessary `unsafe` block");
251-
if let Some((kind, id)) = is_enclosed(cx, blk.id) {
252-
db.span_note(cx.tcx.hir.span(id),
253-
&format!("because it's nested under this `unsafe` {}", kind));
254-
}
255-
db.emit();
256-
}
257-
}
258-
}
259-
}
260-
261207
declare_lint! {
262208
pub PATH_STATEMENTS,
263209
Warn,

src/test/ui/span/lint-unused-unsafe.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,12 @@ note: because it's nested under this `unsafe` block
6565
| |_____^
6666

6767
error: unnecessary `unsafe` block
68-
--> $DIR/lint-unused-unsafe.rs:39:5
68+
--> $DIR/lint-unused-unsafe.rs:40:9
6969
|
70-
39 | / unsafe { //~ ERROR: unnecessary `unsafe` block
71-
40 | | unsafe { //~ ERROR: unnecessary `unsafe` block
70+
40 | / unsafe { //~ ERROR: unnecessary `unsafe` block
7271
41 | | unsf()
7372
42 | | }
74-
43 | | }
75-
| |_____^ unnecessary `unsafe` block
73+
| |_________^ unnecessary `unsafe` block
7674
|
7775
note: because it's nested under this `unsafe` fn
7876
--> $DIR/lint-unused-unsafe.rs:38:1
@@ -87,12 +85,14 @@ note: because it's nested under this `unsafe` fn
8785
| |_^
8886

8987
error: unnecessary `unsafe` block
90-
--> $DIR/lint-unused-unsafe.rs:40:9
88+
--> $DIR/lint-unused-unsafe.rs:39:5
9189
|
92-
40 | / unsafe { //~ ERROR: unnecessary `unsafe` block
90+
39 | / unsafe { //~ ERROR: unnecessary `unsafe` block
91+
40 | | unsafe { //~ ERROR: unnecessary `unsafe` block
9392
41 | | unsf()
9493
42 | | }
95-
| |_________^ unnecessary `unsafe` block
94+
43 | | }
95+
| |_____^ unnecessary `unsafe` block
9696
|
9797
note: because it's nested under this `unsafe` fn
9898
--> $DIR/lint-unused-unsafe.rs:38:1

0 commit comments

Comments
 (0)