Skip to content

Commit dc17f38

Browse files
committed
check_unsafety: more code reuse
1 parent 8a79d08 commit dc17f38

File tree

1 file changed

+18
-39
lines changed

1 file changed

+18
-39
lines changed

src/librustc_mir/transform/check_unsafety.rs

+18-39
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
148148
let cast_out = CastTy::from_ty(cast_ty).expect("bad output type for cast");
149149
match (cast_in, cast_out) {
150150
(CastTy::Ptr(_), CastTy::Int(_)) | (CastTy::FnPtr, CastTy::Int(_)) => {
151-
self.register_violations(
152-
&[UnsafetyViolation {
153-
source_info: self.source_info,
154-
description: Symbol::intern("cast of pointer to int"),
155-
details: Symbol::intern(
156-
"casting pointers to integers in constants",
157-
),
158-
kind: UnsafetyViolationKind::General,
159-
}],
160-
&[],
151+
self.require_unsafe(
152+
"cast of pointer to int",
153+
"casting pointers to integers in constants",
154+
UnsafetyViolationKind::General,
161155
);
162156
}
163157
_ => {}
@@ -171,14 +165,10 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
171165
if self.const_context && self.tcx.features().const_compare_raw_pointers =>
172166
{
173167
if let ty::RawPtr(_) | ty::FnPtr(..) = lhs.ty(self.body, self.tcx).kind {
174-
self.register_violations(
175-
&[UnsafetyViolation {
176-
source_info: self.source_info,
177-
description: Symbol::intern("pointer operation"),
178-
details: Symbol::intern("operations on pointers in constants"),
179-
kind: UnsafetyViolationKind::General,
180-
}],
181-
&[],
168+
self.require_unsafe(
169+
"pointer operation",
170+
"operations on pointers in constants",
171+
UnsafetyViolationKind::General,
182172
);
183173
}
184174
}
@@ -199,18 +189,12 @@ impl<'a, 'tcx> Visitor<'tcx> for UnsafetyChecker<'a, 'tcx> {
199189
.as_ref()
200190
.assert_crate_local()
201191
.lint_root;
202-
self.register_violations(
203-
&[UnsafetyViolation {
204-
source_info,
205-
description: Symbol::intern("borrow of packed field"),
206-
details: Symbol::intern(
207-
"fields of packed structs might be misaligned: dereferencing a \
208-
misaligned pointer or even just creating a misaligned reference \
209-
is undefined behavior",
210-
),
211-
kind: UnsafetyViolationKind::BorrowPacked(lint_root),
212-
}],
213-
&[],
192+
self.require_unsafe(
193+
"borrow of packed field",
194+
"fields of packed structs might be misaligned: dereferencing a \
195+
misaligned pointer or even just creating a misaligned reference \
196+
is undefined behavior",
197+
UnsafetyViolationKind::BorrowPacked(lint_root),
214198
);
215199
}
216200
}
@@ -435,15 +419,10 @@ impl<'a, 'tcx> UnsafetyChecker<'a, 'tcx> {
435419
the field can be changed to invalid values",
436420
)
437421
};
438-
let source_info = self.source_info;
439-
self.register_violations(
440-
&[UnsafetyViolation {
441-
source_info,
442-
description: Symbol::intern(description),
443-
details: Symbol::intern(details),
444-
kind: UnsafetyViolationKind::GeneralAndConstFn,
445-
}],
446-
&[],
422+
self.require_unsafe(
423+
description,
424+
details,
425+
UnsafetyViolationKind::GeneralAndConstFn,
447426
);
448427
}
449428
},

0 commit comments

Comments
 (0)