Skip to content

Add a lint mode for unused unsafe blocks/functions #5797

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

Closed
wants to merge 6 commits into from
Closed
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
52 changes: 25 additions & 27 deletions src/compiletest/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,34 @@ pub fn load_errors(testfile: &Path) -> ~[ExpectedError] {
}

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

// "//~^^^ kind msg" denotes a message expected
// three lines above current line:
let mut adjust_line = 0u;
let len = str::len(line);
while idx < len && line[idx] == ('^' as u8) {
adjust_line += 1u;
idx += 1u;
}
// "//~^^^ kind msg" denotes a message expected
// three lines above current line:
let mut adjust_line = 0u;
let len = str::len(line);
while idx < len && line[idx] == ('^' as u8) {
adjust_line += 1u;
idx += 1u;
}

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

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

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

return ~[ExpectedError{line: line_num - adjust_line, kind: kind,
msg: msg}];
}
return ~[ExpectedError{line: line_num - adjust_line, kind: kind,
msg: msg}];
}
18 changes: 8 additions & 10 deletions src/compiletest/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,14 @@ fn parse_name_directive(line: ~str, directive: ~str) -> bool {

fn parse_name_value_directive(line: ~str,
directive: ~str) -> Option<~str> {
unsafe {
let keycolon = directive + ~":";
match str::find_str(line, keycolon) {
Some(colon) => {
let value = str::slice(line, colon + str::len(keycolon),
str::len(line)).to_owned();
debug!("%s: %s", directive, value);
Some(value)
}
None => None
let keycolon = directive + ~":";
match str::find_str(line, keycolon) {
Some(colon) => {
let value = str::slice(line, colon + str::len(keycolon),
str::len(line)).to_owned();
debug!("%s: %s", directive, value);
Some(value)
}
None => None
}
}
18 changes: 8 additions & 10 deletions src/libcore/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,16 +188,14 @@ impl<T: Owned> Peekable<T> for Port<T> {

#[inline(always)]
fn port_peek<T:Owned>(self: &Port<T>) -> bool {
unsafe {
let mut endp = None;
endp <-> self.endp;
let peek = match &endp {
&Some(ref endp) => peek(endp),
&None => fail!(~"peeking empty stream")
};
self.endp <-> endp;
peek
}
let mut endp = None;
endp <-> self.endp;
let peek = match &endp {
&Some(ref endp) => peek(endp),
&None => fail!(~"peeking empty stream")
};
self.endp <-> endp;
peek
}

impl<T: Owned> Selectable for Port<T> {
Expand Down
23 changes: 9 additions & 14 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,11 +1536,8 @@ pub fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
let mut v = with_bytes_writer(f);

// FIXME (#3758): This should not be needed.
unsafe {
// Make sure the vector has a trailing null and is proper utf8.
v.push(0);
}
// Make sure the vector has a trailing null and is proper utf8.
v.push(0);
assert!(str::is_utf8(v));

unsafe { ::cast::transmute(v) }
Expand Down Expand Up @@ -1640,16 +1637,14 @@ pub mod fsync {
// outer res
pub fn FILE_res_sync(file: &FILERes, opt_level: Option<Level>,
blk: &fn(v: Res<*libc::FILE>)) {
unsafe {
blk(Res(Arg {
val: file.f, opt_level: opt_level,
fsync_fn: |file, l| {
unsafe {
os::fsync_fd(libc::fileno(file), l) as int
}
blk(Res(Arg {
val: file.f, opt_level: opt_level,
fsync_fn: |file, l| {
unsafe {
os::fsync_fd(libc::fileno(file), l) as int
}
}));
}
}
}));
}

// fsync fd after executing blk
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/managed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub mod raw {
#[inline(always)]
pub fn ptr_eq<T>(a: @T, b: @T) -> bool {
//! Determine if two shared boxes point to the same object
unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) }
ptr::addr_of(&(*a)) == ptr::addr_of(&(*b))
}

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

#[cfg(notest)]
Expand Down
12 changes: 6 additions & 6 deletions src/libcore/num/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,27 +369,27 @@ pub fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }

#[inline(always)]
pub fn abs(x: float) -> float {
unsafe { f64::abs(x as f64) as float }
f64::abs(x as f64) as float
}
#[inline(always)]
pub fn sqrt(x: float) -> float {
unsafe { f64::sqrt(x as f64) as float }
f64::sqrt(x as f64) as float
}
#[inline(always)]
pub fn atan(x: float) -> float {
unsafe { f64::atan(x as f64) as float }
f64::atan(x as f64) as float
}
#[inline(always)]
pub fn sin(x: float) -> float {
unsafe { f64::sin(x as f64) as float }
f64::sin(x as f64) as float
}
#[inline(always)]
pub fn cos(x: float) -> float {
unsafe { f64::cos(x as f64) as float }
f64::cos(x as f64) as float
}
#[inline(always)]
pub fn tan(x: float) -> float {
unsafe { f64::tan(x as f64) as float }
f64::tan(x as f64) as float
}

#[cfg(notest)]
Expand Down
60 changes: 25 additions & 35 deletions src/libcore/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,11 @@ impl GenericPath for PosixPath {
}

fn dirname(&self) -> ~str {
unsafe {
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
}
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
}
}

Expand Down Expand Up @@ -439,10 +437,8 @@ impl GenericPath for PosixPath {
}

fn with_filename(&self, f: &str) -> PosixPath {
unsafe {
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
self.dir_path().push(f)
}
assert!(! str::any(f, |c| windows::is_sep(c as u8)));
self.dir_path().push(f)
}

fn with_filestem(&self, s: &str) -> PosixPath {
Expand Down Expand Up @@ -509,7 +505,7 @@ impl GenericPath for PosixPath {
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
v.push_all_move(ss);
}
PosixPath { is_absolute: self.is_absolute,
components: v }
Expand All @@ -521,14 +517,14 @@ impl GenericPath for PosixPath {
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
v.push_all_move(ss);
PosixPath { components: v, ..copy *self }
}

fn pop(&self) -> PosixPath {
let mut cs = copy self.components;
if cs.len() != 0 {
unsafe { cs.pop(); }
cs.pop();
}
return PosixPath {
is_absolute: self.is_absolute,
Expand Down Expand Up @@ -607,13 +603,11 @@ impl GenericPath for WindowsPath {
}

fn dirname(&self) -> ~str {
unsafe {
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
}
let s = self.dir_path().to_str();
if s.len() == 0 {
~"."
} else {
s
}
}

Expand Down Expand Up @@ -770,7 +764,7 @@ impl GenericPath for WindowsPath {
for str::each_split_nonempty(*e, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
v.push_all_move(ss);
}
// tedious, but as-is, we can't use ..self
return WindowsPath {
Expand All @@ -787,14 +781,14 @@ impl GenericPath for WindowsPath {
for str::each_split_nonempty(s, |c| windows::is_sep(c as u8)) |s| {
ss.push(s.to_owned())
}
unsafe { v.push_all_move(ss); }
v.push_all_move(ss);
return WindowsPath { components: v, ..copy *self }
}

fn pop(&self) -> WindowsPath {
let mut cs = copy self.components;
if cs.len() != 0 {
unsafe { cs.pop(); }
cs.pop();
}
return WindowsPath {
host: copy self.host,
Expand All @@ -820,18 +814,14 @@ impl GenericPath for WindowsPath {

pub fn normalize(components: &[~str]) -> ~[~str] {
let mut cs = ~[];
unsafe {
for components.each |c| {
unsafe {
if *c == ~"." && components.len() > 1 { loop; }
if *c == ~"" { loop; }
if *c == ~".." && cs.len() != 0 {
cs.pop();
loop;
}
cs.push(copy *c);
}
for components.each |c| {
if *c == ~"." && components.len() > 1 { loop; }
if *c == ~"" { loop; }
if *c == ~".." && cs.len() != 0 {
cs.pop();
loop;
}
cs.push(copy *c);
}
cs
}
Expand Down
8 changes: 2 additions & 6 deletions src/libcore/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,13 @@ pub fn addr_of<T>(val: &T) -> *T { unsafe { rusti::addr_of(*val) } }
/// Calculate the offset from a pointer
#[inline(always)]
pub fn offset<T>(ptr: *T, count: uint) -> *T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}
(ptr as uint + count * sys::size_of::<T>()) as *T
}

/// Calculate the offset from a const pointer
#[inline(always)]
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
unsafe {
(ptr as uint + count * sys::size_of::<T>()) as *T
}
(ptr as uint + count * sys::size_of::<T>()) as *T
}

/// Calculate the offset from a mut pointer
Expand Down
4 changes: 1 addition & 3 deletions src/libcore/rt/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ fn align_down(sp: *mut uint) -> *mut uint {
#[inline(always)]
pub fn mut_offset<T>(ptr: *mut T, count: int) -> *mut T {
use core::sys::size_of;
unsafe {
(ptr as int + count * (size_of::<T>() as int)) as *mut T
}
(ptr as int + count * (size_of::<T>() as int)) as *mut T
}

6 changes: 3 additions & 3 deletions src/libcore/rt/thread_local_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ pub type Key = pthread_key_t;

#[cfg(unix)]
pub unsafe fn create(key: &mut Key) {
unsafe { assert!(0 == pthread_key_create(key, null())); }
assert!(0 == pthread_key_create(key, null()));
}

#[cfg(unix)]
pub unsafe fn set(key: Key, value: *mut c_void) {
unsafe { assert!(0 == pthread_setspecific(key, value)); }
assert!(0 == pthread_setspecific(key, value));
}

#[cfg(unix)]
pub unsafe fn get(key: Key) -> *mut c_void {
unsafe { pthread_getspecific(key) }
pthread_getspecific(key)
}

#[cfg(target_os="macos")]
Expand Down
Loading