Skip to content

Commit 4beebc4

Browse files
committed
auto merge of #5797 : alexcrichton/rust/issue-1913, r=catamorphism
Closes #5487, #1913, and #4568 I tracked this by adding all used unsafe blocks/functions to a set on the `tcx` passed around, and then when the lint pass comes around if an unsafe block/function isn't listed in that set, it's unused. I also removed everything from the compiler that was unused, and up to stage2 is now compiling without any known unused unsafe blocks. I chose `unused_unsafe` as the name of the lint attribute, but there may be a better name...
2 parents 39e2ab5 + 59e69aa commit 4beebc4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1045
-985
lines changed

src/compiletest/errors.rs

+25-27
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,34 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
3030
}
3131

3232
fn parse_expected(line_num: uint, line: ~str) -> ~[ExpectedError] {
33-
unsafe {
34-
let error_tag = ~"//~";
35-
let mut idx;
36-
match str::find_str(line, error_tag) {
37-
None => return ~[],
38-
Some(nn) => { idx = (nn as uint) + str::len(error_tag); }
39-
}
33+
let error_tag = ~"//~";
34+
let mut idx;
35+
match str::find_str(line, error_tag) {
36+
None => return ~[],
37+
Some(nn) => { idx = (nn as uint) + str::len(error_tag); }
38+
}
4039

41-
// "//~^^^ kind msg" denotes a message expected
42-
// three lines above current line:
43-
let mut adjust_line = 0u;
44-
let len = str::len(line);
45-
while idx < len && line[idx] == ('^' as u8) {
46-
adjust_line += 1u;
47-
idx += 1u;
48-
}
40+
// "//~^^^ kind msg" denotes a message expected
41+
// three lines above current line:
42+
let mut adjust_line = 0u;
43+
let len = str::len(line);
44+
while idx < len && line[idx] == ('^' as u8) {
45+
adjust_line += 1u;
46+
idx += 1u;
47+
}
4948

50-
// Extract kind:
51-
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
52-
let start_kind = idx;
53-
while idx < len && line[idx] != (' ' as u8) { idx += 1u; }
54-
let kind = str::to_lower(str::slice(line, start_kind, idx).to_owned());
49+
// Extract kind:
50+
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
51+
let start_kind = idx;
52+
while idx < len && line[idx] != (' ' as u8) { idx += 1u; }
53+
let kind = str::to_lower(str::slice(line, start_kind, idx).to_owned());
5554

56-
// Extract msg:
57-
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
58-
let msg = str::slice(line, idx, len).to_owned();
55+
// Extract msg:
56+
while idx < len && line[idx] == (' ' as u8) { idx += 1u; }
57+
let msg = str::slice(line, idx, len).to_owned();
5958

60-
debug!("line=%u kind=%s msg=%s", line_num - adjust_line, kind, msg);
59+
debug!("line=%u kind=%s msg=%s", line_num - adjust_line, kind, msg);
6160

62-
return ~[ExpectedError{line: line_num - adjust_line, kind: kind,
63-
msg: msg}];
64-
}
61+
return ~[ExpectedError{line: line_num - adjust_line, kind: kind,
62+
msg: msg}];
6563
}

src/compiletest/header.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,14 @@ fn parse_name_directive(line: ~str, directive: ~str) -> bool {
171171

172172
fn parse_name_value_directive(line: ~str,
173173
directive: ~str) -> Option<~str> {
174-
unsafe {
175-
let keycolon = directive + ~":";
176-
match str::find_str(line, keycolon) {
177-
Some(colon) => {
178-
let value = str::slice(line, colon + str::len(keycolon),
179-
str::len(line)).to_owned();
180-
debug!("%s: %s", directive, value);
181-
Some(value)
182-
}
183-
None => None
174+
let keycolon = directive + ~":";
175+
match str::find_str(line, keycolon) {
176+
Some(colon) => {
177+
let value = str::slice(line, colon + str::len(keycolon),
178+
str::len(line)).to_owned();
179+
debug!("%s: %s", directive, value);
180+
Some(value)
184181
}
182+
None => None
185183
}
186184
}

src/libcore/comm.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,14 @@ impl<T: Owned> Peekable<T> for Port<T> {
188188
189189
#[inline(always)]
190190
fn port_peek<T:Owned>(self: &Port<T>) -> bool {
191-
unsafe {
192-
let mut endp = None;
193-
endp <-> self.endp;
194-
let peek = match &endp {
195-
&Some(ref endp) => peek(endp),
196-
&None => fail!(~"peeking empty stream")
197-
};
198-
self.endp <-> endp;
199-
peek
200-
}
191+
let mut endp = None;
192+
endp <-> self.endp;
193+
let peek = match &endp {
194+
&Some(ref endp) => peek(endp),
195+
&None => fail!(~"peeking empty stream")
196+
};
197+
self.endp <-> endp;
198+
peek
201199
}
202200
203201
impl<T: Owned> Selectable for Port<T> {

src/libcore/io.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -1536,11 +1536,8 @@ pub fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
15361536
pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
15371537
let mut v = with_bytes_writer(f);
15381538
1539-
// FIXME (#3758): This should not be needed.
1540-
unsafe {
1541-
// Make sure the vector has a trailing null and is proper utf8.
1542-
v.push(0);
1543-
}
1539+
// Make sure the vector has a trailing null and is proper utf8.
1540+
v.push(0);
15441541
assert!(str::is_utf8(v));
15451542
15461543
unsafe { ::cast::transmute(v) }
@@ -1640,16 +1637,14 @@ pub mod fsync {
16401637
// outer res
16411638
pub fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>,
16421639
blk: &fn(v: Res<*libc::FILE>)) {
1643-
unsafe {
1644-
blk(Res(Arg {
1645-
val: file.f, opt_level: opt_level,
1646-
fsync_fn: |file, l| {
1647-
unsafe {
1648-
os::fsync_fd(libc::fileno(file), l) as int
1649-
}
1640+
blk(Res(Arg {
1641+
val: file.f, opt_level: opt_level,
1642+
fsync_fn: |file, l| {
1643+
unsafe {
1644+
os::fsync_fd(libc::fileno(file), l) as int
16501645
}
1651-
}));
1652-
}
1646+
}
1647+
}));
16531648
}
16541649

16551650
// fsync fd after executing blk

src/libcore/managed.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ pub mod raw {
3838
#[inline(always)]
3939
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
4040
//! Determine if two shared boxes point to the same object
41-
unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) }
41+
ptr::addr_of(&(*a)) == ptr::addr_of(&(*b))
4242
}
4343

4444
#[inline(always)]
4545
pub fn mut_ptr_eq<T>(a: @mut T, b: @mut T) -> bool {
4646
//! Determine if two mutable shared boxes point to the same object
47-
unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) }
47+
ptr::addr_of(&(*a)) == ptr::addr_of(&(*b))
4848
}
4949

5050
#[cfg(notest)]

src/libcore/num/float.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -369,27 +369,27 @@ pub fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
369369
370370
#[inline(always)]
371371
pub fn abs(x: float) -> float {
372-
unsafe { f64::abs(x as f64) as float }
372+
f64::abs(x as f64) as float
373373
}
374374
#[inline(always)]
375375
pub fn sqrt(x: float) -> float {
376-
unsafe { f64::sqrt(x as f64) as float }
376+
f64::sqrt(x as f64) as float
377377
}
378378
#[inline(always)]
379379
pub fn atan(x: float) -> float {
380-
unsafe { f64::atan(x as f64) as float }
380+
f64::atan(x as f64) as float
381381
}
382382
#[inline(always)]
383383
pub fn sin(x: float) -> float {
384-
unsafe { f64::sin(x as f64) as float }
384+
f64::sin(x as f64) as float
385385
}
386386
#[inline(always)]
387387
pub fn cos(x: float) -> float {
388-
unsafe { f64::cos(x as f64) as float }
388+
f64::cos(x as f64) as float
389389
}
390390
#[inline(always)]
391391
pub fn tan(x: float) -> float {
392-
unsafe { f64::tan(x as f64) as float }
392+
f64::tan(x as f64) as float
393393
}
394394
395395
#[cfg(notest)]

src/libcore/path.rs

+25-35
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,11 @@ impl GenericPath for PosixPath {
389389
}
390390

391391
fn dirname(&self) -> ~str {
392-
unsafe {
393-
let s = self.dir_path().to_str();
394-
if s.len() == 0 {
395-
~"."
396-
} else {
397-
s
398-
}
392+
let s = self.dir_path().to_str();
393+
if s.len() == 0 {
394+
~"."
395+
} else {
396+
s
399397
}
400398
}
401399

@@ -439,10 +437,8 @@ impl GenericPath for PosixPath {
439437
}
440438

441439
fn with_filename(&self, f: &str) -> PosixPath {
442-
unsafe {
443-
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
444-
self.dir_path().push(f)
445-
}
440+
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
441+
self.dir_path().push(f)
446442
}
447443

448444
fn with_filestem(&self, s: &str) -> PosixPath {
@@ -509,7 +505,7 @@ impl GenericPath for PosixPath {
509505
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
510506
ss.push(s.to_owned())
511507
}
512-
unsafe { v.push_all_move(ss); }
508+
v.push_all_move(ss);
513509
}
514510
PosixPath { is_absolute: self.is_absolute,
515511
components: v }
@@ -521,14 +517,14 @@ impl GenericPath for PosixPath {
521517
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
522518
ss.push(s.to_owned())
523519
}
524-
unsafe { v.push_all_move(ss); }
520+
v.push_all_move(ss);
525521
PosixPath { components: v, ..copy *self }
526522
}
527523

528524
fn pop(&self) -> PosixPath {
529525
let mut cs = copy self.components;
530526
if cs.len() != 0 {
531-
unsafe { cs.pop(); }
527+
cs.pop();
532528
}
533529
return PosixPath {
534530
is_absolute: self.is_absolute,
@@ -607,13 +603,11 @@ impl GenericPath for WindowsPath {
607603
}
608604

609605
fn dirname(&self) -> ~str {
610-
unsafe {
611-
let s = self.dir_path().to_str();
612-
if s.len() == 0 {
613-
~"."
614-
} else {
615-
s
616-
}
606+
let s = self.dir_path().to_str();
607+
if s.len() == 0 {
608+
~"."
609+
} else {
610+
s
617611
}
618612
}
619613

@@ -770,7 +764,7 @@ impl GenericPath for WindowsPath {
770764
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
771765
ss.push(s.to_owned())
772766
}
773-
unsafe { v.push_all_move(ss); }
767+
v.push_all_move(ss);
774768
}
775769
// tedious, but as-is, we can't use ..self
776770
return WindowsPath {
@@ -787,14 +781,14 @@ impl GenericPath for WindowsPath {
787781
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
788782
ss.push(s.to_owned())
789783
}
790-
unsafe { v.push_all_move(ss); }
784+
v.push_all_move(ss);
791785
return WindowsPath { components: v, ..copy *self }
792786
}
793787

794788
fn pop(&self) -> WindowsPath {
795789
let mut cs = copy self.components;
796790
if cs.len() != 0 {
797-
unsafe { cs.pop(); }
791+
cs.pop();
798792
}
799793
return WindowsPath {
800794
host: copy self.host,
@@ -820,18 +814,14 @@ impl GenericPath for WindowsPath {
820814

821815
pub fn normalize(components: &[~str]) -> ~[~str] {
822816
let mut cs = ~[];
823-
unsafe {
824-
for components.each |c| {
825-
unsafe {
826-
if *c == ~"." && components.len() > 1 { loop; }
827-
if *c == ~"" { loop; }
828-
if *c == ~".." && cs.len() != 0 {
829-
cs.pop();
830-
loop;
831-
}
832-
cs.push(copy *c);
833-
}
817+
for components.each |c| {
818+
if *c == ~"." && components.len() > 1 { loop; }
819+
if *c == ~"" { loop; }
820+
if *c == ~".." && cs.len() != 0 {
821+
cs.pop();
822+
loop;
834823
}
824+
cs.push(copy *c);
835825
}
836826
cs
837827
}

src/libcore/ptr.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,13 @@ pub fn addr_of<T>(val: &T) -> *T { unsafe { rusti::addr_of(*val) } }
5555
/// Calculate the offset from a pointer
5656
#[inline(always)]
5757
pub fn offset<T>(ptr: *T, count: uint) -> *T {
58-
unsafe {
59-
(ptr as uint + count * sys::size_of::<T>()) as *T
60-
}
58+
(ptr as uint + count * sys::size_of::<T>()) as *T
6159
}
6260

6361
/// Calculate the offset from a const pointer
6462
#[inline(always)]
6563
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
66-
unsafe {
67-
(ptr as uint + count * sys::size_of::<T>()) as *T
68-
}
64+
(ptr as uint + count * sys::size_of::<T>()) as *T
6965
}
7066

7167
/// Calculate the offset from a mut pointer

src/libcore/rt/context.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,6 @@ fn align_down(sp: *mut uint) -> *mut uint {
205205
#[inline(always)]
206206
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
207207
use core::sys::size_of;
208-
unsafe {
209-
(ptr as int + count * (size_of::<T>() as int)) as *mut T
210-
}
208+
(ptr as int + count * (size_of::<T>() as int)) as *mut T
211209
}
212210

src/libcore/rt/thread_local_storage.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ pub type Key = pthread_key_t;
2121

2222
#[cfg(unix)]
2323
pub unsafe fn create(key: &mut Key) {
24-
unsafe { assert!(0 == pthread_key_create(key, null())); }
24+
assert!(0 == pthread_key_create(key, null()));
2525
}
2626

2727
#[cfg(unix)]
2828
pub unsafe fn set(key: Key, value: *mut c_void) {
29-
unsafe { assert!(0 == pthread_setspecific(key, value)); }
29+
assert!(0 == pthread_setspecific(key, value));
3030
}
3131

3232
#[cfg(unix)]
3333
pub unsafe fn get(key: Key) -> *mut c_void {
34-
unsafe { pthread_getspecific(key) }
34+
pthread_getspecific(key)
3535
}
3636

3737
#[cfg(target_os="macos")]

0 commit comments

Comments
 (0)