Skip to content

Commit 68e3f04

Browse files
committed
---
yaml --- r: 55710 b: refs/heads/master c: 276293a h: refs/heads/master v: v3
1 parent 195c73b commit 68e3f04

File tree

5 files changed

+53
-28
lines changed

5 files changed

+53
-28
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: f0afe23dce9699f2776907d2adde126baea58f30
2+
refs/heads/master: 276293af7c0fb901c8344e88562ce635f26f47a9
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
55
refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a

trunk/src/libcore/char.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -234,6 +234,21 @@ pub fn escape_default(c: char) -> ~str {
234234
}
235235
}
236236

237+
/// Returns the amount of bytes this character would need if encoded in utf8
238+
pub fn len_utf8_bytes(c: char) -> uint {
239+
static max_one_b: uint = 128u;
240+
static max_two_b: uint = 2048u;
241+
static max_three_b: uint = 65536u;
242+
static max_four_b: uint = 2097152u;
243+
244+
let code = c as uint;
245+
if code < max_one_b { 1u }
246+
else if code < max_two_b { 2u }
247+
else if code < max_three_b { 3u }
248+
else if code < max_four_b { 4u }
249+
else { fail!(~"invalid character!") }
250+
}
251+
237252
/**
238253
* Compare two chars
239254
*
@@ -334,7 +349,6 @@ fn test_escape_default() {
334349
assert_eq!(escape_default('\U0001d4b6'), ~"\\U0001d4b6");
335350
}
336351
337-
338352
#[test]
339353
fn test_escape_unicode() {
340354
assert_eq!(escape_unicode('\x00'), ~"\\x00");

trunk/src/libcore/str.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -789,16 +789,18 @@ pub fn each_split_within<'a>(ss: &'a str,
789789

790790
/// Convert a string to lowercase. ASCII only
791791
pub fn to_lower(s: &str) -> ~str {
792-
map(s,
793-
|c| unsafe{(libc::tolower(c as libc::c_char)) as char}
794-
)
792+
do map(s) |c| {
793+
assert!(char::is_ascii(c));
794+
(unsafe{libc::tolower(c as libc::c_char)}) as char
795+
}
795796
}
796797

797798
/// Convert a string to uppercase. ASCII only
798799
pub fn to_upper(s: &str) -> ~str {
799-
map(s,
800-
|c| unsafe{(libc::toupper(c as libc::c_char)) as char}
801-
)
800+
do map(s) |c| {
801+
assert!(char::is_ascii(c));
802+
(unsafe{libc::toupper(c as libc::c_char)}) as char
803+
}
802804
}
803805

804806
/**
@@ -3096,12 +3098,11 @@ mod tests {
30963098
30973099
#[test]
30983100
fn test_to_lower() {
3099-
unsafe {
3100-
assert!(~"" == map(~"",
3101-
|c| libc::tolower(c as c_char) as char));
3102-
assert!(~"ymca" == map(~"YMCA",
3103-
|c| libc::tolower(c as c_char) as char));
3104-
}
3101+
// libc::tolower, and hence str::to_lower
3102+
// are culturally insensitive: they only work for ASCII
3103+
// (see Issue #1347)
3104+
assert!(~"" == to_lower(""));
3105+
assert!(~"ymca" == to_lower("YMCA"));
31053106
}
31063107
31073108
#[test]
@@ -3666,12 +3667,8 @@ mod tests {
36663667

36673668
#[test]
36683669
fn test_map() {
3669-
unsafe {
3670-
assert!(~"" == map(~"", |c|
3671-
libc::toupper(c as c_char) as char));
3672-
assert!(~"YMCA" == map(~"ymca",
3673-
|c| libc::toupper(c as c_char) as char));
3674-
}
3670+
assert!(~"" == map(~"", |c| unsafe {libc::toupper(c as c_char)} as char));
3671+
assert!(~"YMCA" == map(~"ymca", |c| unsafe {libc::toupper(c as c_char)} as char));
36753672
}
36763673
36773674
#[test]
@@ -3685,11 +3682,11 @@ mod tests {
36853682
36863683
#[test]
36873684
fn test_any() {
3688-
assert!(false == any(~"", char::is_uppercase));
3685+
assert!(false == any(~"", char::is_uppercase));
36893686
assert!(false == any(~"ymca", char::is_uppercase));
36903687
assert!(true == any(~"YMCA", char::is_uppercase));
3691-
assert!(true == any(~"yMCA", char::is_uppercase));
3692-
assert!(true == any(~"Ymcy", char::is_uppercase));
3688+
assert!(true == any(~"yMCA", char::is_uppercase));
3689+
assert!(true == any(~"Ymcy", char::is_uppercase));
36933690
}
36943691
36953692
#[test]

trunk/src/librustc/middle/lint.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ pub fn get_lint_dict() -> LintDict {
260260
(~"unused_unsafe",
261261
LintSpec {
262262
lint: unused_unsafe,
263-
desc: "unnecessary use of an \"unsafe\" block",
263+
desc: "unnecessary use of an \"unsafe\" block or function",
264264
default: warn
265265
}),
266266

@@ -958,6 +958,17 @@ fn check_fn(tcx: ty::ctxt, fk: &visit::fn_kind, decl: &ast::fn_decl,
958958
_body: &ast::blk, span: span, id: ast::node_id) {
959959
debug!("lint check_fn fk=%? id=%?", fk, id);
960960

961+
// Check for an 'unsafe fn' which doesn't need to be unsafe
962+
match *fk {
963+
visit::fk_item_fn(_, _, ast::unsafe_fn, _) => {
964+
if !tcx.used_unsafe.contains(&id) {
965+
tcx.sess.span_lint(unused_unsafe, id, id, span,
966+
~"unnecessary \"unsafe\" function");
967+
}
968+
}
969+
_ => ()
970+
}
971+
961972
// Check for deprecated modes
962973
match *fk {
963974
// don't complain about blocks, since they tend to get their modes

trunk/src/test/compile-fail/unused-unsafe.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ fn callback<T>(_f: &fn() -> T) -> T { fail!() }
1818

1919
fn bad1() { unsafe {} } //~ ERROR: unnecessary "unsafe" block
2020
fn bad2() { unsafe { bad1() } } //~ ERROR: unnecessary "unsafe" block
21-
unsafe fn bad4() { unsafe {} } //~ ERROR: unnecessary "unsafe" block
21+
unsafe fn bad3() {} //~ ERROR: unnecessary "unsafe" function
22+
unsafe fn bad4() { unsafe {} } //~ ERROR: unnecessary "unsafe" function
23+
//~^ ERROR: unnecessary "unsafe" block
2224
fn bad5() { unsafe { do callback {} } } //~ ERROR: unnecessary "unsafe" block
2325

2426
unsafe fn good0() { libc::exit(1) }
@@ -36,6 +38,7 @@ fn good2() {
3638
}
3739
}
3840

39-
#[allow(unused_unsafe)] fn allowed() { unsafe {} }
41+
#[allow(unused_unsafe)] unsafe fn allowed0() {}
42+
#[allow(unused_unsafe)] fn allowed1() { unsafe {} }
4043

4144
fn main() { }

0 commit comments

Comments
 (0)