Skip to content

Commit b62b1b6

Browse files
authored
Merge pull request #2126 from camsteffen/split-tests
Split some UI tests
2 parents 408b522 + 35882b0 commit b62b1b6

16 files changed

+712
-731
lines changed

tests/ui/cstring.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {}
2+
3+
#[allow(result_unwrap_used)]
4+
fn temporary_cstring() {
5+
use std::ffi::CString;
6+
7+
CString::new("foo").unwrap().as_ptr();
8+
}

tests/ui/cstring.stderr

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error: function is never used: `temporary_cstring`
2+
--> $DIR/cstring.rs:4:1
3+
|
4+
4 | fn temporary_cstring() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D dead-code` implied by `-D warnings`
8+
9+
error: you are getting the inner pointer of a temporary `CString`
10+
--> $DIR/cstring.rs:7:5
11+
|
12+
7 | CString::new("foo").unwrap().as_ptr();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= note: `-D temporary-cstring-as-ptr` implied by `-D warnings`
16+
= note: that pointer will be invalid outside this expression
17+
help: assign the `CString` to a variable to extend its lifetime
18+
--> $DIR/cstring.rs:7:5
19+
|
20+
7 | CString::new("foo").unwrap().as_ptr();
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22+

tests/ui/get_unwrap.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![allow(unused_mut)]
2+
3+
use std::collections::BTreeMap;
4+
use std::collections::HashMap;
5+
use std::collections::VecDeque;
6+
use std::iter::FromIterator;
7+
8+
struct GetFalsePositive {
9+
arr: [u32; 3],
10+
}
11+
12+
impl GetFalsePositive {
13+
fn get(&self, pos: usize) -> Option<&u32> { self.arr.get(pos) }
14+
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { self.arr.get_mut(pos) }
15+
}
16+
17+
fn main() {
18+
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
19+
let mut some_slice = &mut [0, 1, 2, 3];
20+
let mut some_vec = vec![0, 1, 2, 3];
21+
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
22+
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
23+
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
24+
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
25+
26+
{ // Test `get().unwrap()`
27+
let _ = boxed_slice.get(1).unwrap();
28+
let _ = some_slice.get(0).unwrap();
29+
let _ = some_vec.get(0).unwrap();
30+
let _ = some_vecdeque.get(0).unwrap();
31+
let _ = some_hashmap.get(&1).unwrap();
32+
let _ = some_btreemap.get(&1).unwrap();
33+
let _ = false_positive.get(0).unwrap();
34+
}
35+
36+
{ // Test `get_mut().unwrap()`
37+
*boxed_slice.get_mut(0).unwrap() = 1;
38+
*some_slice.get_mut(0).unwrap() = 1;
39+
*some_vec.get_mut(0).unwrap() = 1;
40+
*some_vecdeque.get_mut(0).unwrap() = 1;
41+
// Check false positives
42+
*some_hashmap.get_mut(&1).unwrap() = 'b';
43+
*some_btreemap.get_mut(&1).unwrap() = 'b';
44+
*false_positive.get_mut(0).unwrap() = 1;
45+
}
46+
}

tests/ui/get_unwrap.stderr

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
2+
--> $DIR/get_unwrap.rs:27:17
3+
|
4+
27 | let _ = boxed_slice.get(1).unwrap();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
6+
|
7+
= note: `-D get-unwrap` implied by `-D warnings`
8+
9+
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
10+
--> $DIR/get_unwrap.rs:28:17
11+
|
12+
28 | let _ = some_slice.get(0).unwrap();
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`
14+
15+
error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
16+
--> $DIR/get_unwrap.rs:29:17
17+
|
18+
29 | let _ = some_vec.get(0).unwrap();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]`
20+
21+
error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
22+
--> $DIR/get_unwrap.rs:30:17
23+
|
24+
30 | let _ = some_vecdeque.get(0).unwrap();
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]`
26+
27+
error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
28+
--> $DIR/get_unwrap.rs:31:17
29+
|
30+
31 | let _ = some_hashmap.get(&1).unwrap();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]`
32+
33+
error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
34+
--> $DIR/get_unwrap.rs:32:17
35+
|
36+
32 | let _ = some_btreemap.get(&1).unwrap();
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]`
38+
39+
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
40+
--> $DIR/get_unwrap.rs:37:10
41+
|
42+
37 | *boxed_slice.get_mut(0).unwrap() = 1;
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut boxed_slice[0]`
44+
45+
error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
46+
--> $DIR/get_unwrap.rs:38:10
47+
|
48+
38 | *some_slice.get_mut(0).unwrap() = 1;
49+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_slice[0]`
50+
51+
error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
52+
--> $DIR/get_unwrap.rs:39:10
53+
|
54+
39 | *some_vec.get_mut(0).unwrap() = 1;
55+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vec[0]`
56+
57+
error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
58+
--> $DIR/get_unwrap.rs:40:10
59+
|
60+
40 | *some_vecdeque.get_mut(0).unwrap() = 1;
61+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vecdeque[0]`
62+

tests/ui/methods.rs

Lines changed: 0 additions & 233 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,6 @@ impl IteratorFalsePositives {
189189
}
190190
}
191191

192-
#[derive(Copy, Clone)]
193-
struct HasChars;
194-
195-
impl HasChars {
196-
fn chars(self) -> std::str::Chars<'static> {
197-
"HasChars".chars()
198-
}
199-
}
200-
201192
/// Checks implementation of `FILTER_NEXT` lint
202193
fn filter_next() {
203194
let v = vec![3, 2, 1, 0, -1, -2, -3];
@@ -358,232 +349,8 @@ fn iter_skip_next() {
358349
let _ = foo.filter().skip(42).next();
359350
}
360351

361-
struct GetFalsePositive {
362-
arr: [u32; 3],
363-
}
364-
365-
impl GetFalsePositive {
366-
fn get(&self, pos: usize) -> Option<&u32> { self.arr.get(pos) }
367-
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { self.arr.get_mut(pos) }
368-
}
369-
370-
/// Checks implementation of `GET_UNWRAP` lint
371-
fn get_unwrap() {
372-
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
373-
let mut some_slice = &mut [0, 1, 2, 3];
374-
let mut some_vec = vec![0, 1, 2, 3];
375-
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
376-
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
377-
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
378-
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };
379-
380-
{ // Test `get().unwrap()`
381-
let _ = boxed_slice.get(1).unwrap();
382-
let _ = some_slice.get(0).unwrap();
383-
let _ = some_vec.get(0).unwrap();
384-
let _ = some_vecdeque.get(0).unwrap();
385-
let _ = some_hashmap.get(&1).unwrap();
386-
let _ = some_btreemap.get(&1).unwrap();
387-
let _ = false_positive.get(0).unwrap();
388-
}
389-
390-
{ // Test `get_mut().unwrap()`
391-
*boxed_slice.get_mut(0).unwrap() = 1;
392-
*some_slice.get_mut(0).unwrap() = 1;
393-
*some_vec.get_mut(0).unwrap() = 1;
394-
*some_vecdeque.get_mut(0).unwrap() = 1;
395-
// Check false positives
396-
*some_hashmap.get_mut(&1).unwrap() = 'b';
397-
*some_btreemap.get_mut(&1).unwrap() = 'b';
398-
*false_positive.get_mut(0).unwrap() = 1;
399-
}
400-
}
401-
402-
403352
#[allow(similar_names)]
404353
fn main() {
405-
use std::io;
406-
407354
let opt = Some(0);
408355
let _ = opt.unwrap();
409-
410-
let res: Result<i32, ()> = Ok(0);
411-
let _ = res.unwrap();
412-
413-
res.ok().expect("disaster!");
414-
// the following should not warn, since `expect` isn't implemented unless
415-
// the error type implements `Debug`
416-
let res2: Result<i32, MyError> = Ok(0);
417-
res2.ok().expect("oh noes!");
418-
let res3: Result<u32, MyErrorWithParam<u8>>= Ok(0);
419-
res3.ok().expect("whoof");
420-
let res4: Result<u32, io::Error> = Ok(0);
421-
res4.ok().expect("argh");
422-
let res5: io::Result<u32> = Ok(0);
423-
res5.ok().expect("oops");
424-
let res6: Result<u32, &str> = Ok(0);
425-
res6.ok().expect("meh");
426-
}
427-
428-
struct MyError(()); // doesn't implement Debug
429-
430-
#[derive(Debug)]
431-
struct MyErrorWithParam<T> {
432-
x: T
433-
}
434-
435-
#[allow(unnecessary_operation)]
436-
fn starts_with() {
437-
"".chars().next() == Some(' ');
438-
Some(' ') != "".chars().next();
439-
}
440-
441-
fn str_extend_chars() {
442-
let abc = "abc";
443-
let def = String::from("def");
444-
let mut s = String::new();
445-
446-
s.push_str(abc);
447-
s.extend(abc.chars());
448-
449-
s.push_str("abc");
450-
s.extend("abc".chars());
451-
452-
s.push_str(&def);
453-
s.extend(def.chars());
454-
455-
s.extend(abc.chars().skip(1));
456-
s.extend("abc".chars().skip(1));
457-
s.extend(['a', 'b', 'c'].iter());
458-
459-
let f = HasChars;
460-
s.extend(f.chars());
461-
}
462-
463-
fn clone_on_copy() {
464-
42.clone();
465-
466-
vec![1].clone(); // ok, not a Copy type
467-
Some(vec![1]).clone(); // ok, not a Copy type
468-
(&42).clone();
469-
}
470-
471-
fn clone_on_ref_ptr() {
472-
let rc = Rc::new(true);
473-
let arc = Arc::new(true);
474-
475-
let rcweak = Rc::downgrade(&rc);
476-
let arc_weak = Arc::downgrade(&arc);
477-
478-
rc.clone();
479-
Rc::clone(&rc);
480-
481-
arc.clone();
482-
Arc::clone(&arc);
483-
484-
rcweak.clone();
485-
rc::Weak::clone(&rcweak);
486-
487-
arc_weak.clone();
488-
sync::Weak::clone(&arc_weak);
489-
490-
491-
}
492-
493-
fn clone_on_copy_generic<T: Copy>(t: T) {
494-
t.clone();
495-
496-
Some(t).clone();
497-
}
498-
499-
fn clone_on_double_ref() {
500-
let x = vec![1];
501-
let y = &&x;
502-
let z: &Vec<_> = y.clone();
503-
504-
println!("{:p} {:p}",*y, z);
505-
}
506-
507-
fn single_char_pattern() {
508-
let x = "foo";
509-
x.split("x");
510-
x.split("xx");
511-
x.split('x');
512-
513-
let y = "x";
514-
x.split(y);
515-
// Not yet testing for multi-byte characters
516-
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern`
517-
// should have done this but produced an ICE
518-
//
519-
// We may not want to suggest changing these anyway
520-
// See: https://github.com/rust-lang-nursery/rust-clippy/issues/650#issuecomment-184328984
521-
x.split("ß");
522-
x.split("ℝ");
523-
x.split("💣");
524-
// Can't use this lint for unicode code points which don't fit in a char
525-
x.split("❤️");
526-
x.contains("x");
527-
x.starts_with("x");
528-
x.ends_with("x");
529-
x.find("x");
530-
x.rfind("x");
531-
x.rsplit("x");
532-
x.split_terminator("x");
533-
x.rsplit_terminator("x");
534-
x.splitn(0, "x");
535-
x.rsplitn(0, "x");
536-
x.matches("x");
537-
x.rmatches("x");
538-
x.match_indices("x");
539-
x.rmatch_indices("x");
540-
x.trim_left_matches("x");
541-
x.trim_right_matches("x");
542-
543-
let h = HashSet::<String>::new();
544-
h.contains("X"); // should not warn
545-
}
546-
547-
#[allow(result_unwrap_used)]
548-
fn temporary_cstring() {
549-
use std::ffi::CString;
550-
551-
CString::new("foo").unwrap().as_ptr();
552-
}
553-
554-
fn iter_clone_collect() {
555-
let v = [1,2,3,4,5];
556-
let v2 : Vec<isize> = v.iter().cloned().collect();
557-
let v3 : HashSet<isize> = v.iter().cloned().collect();
558-
let v4 : VecDeque<isize> = v.iter().cloned().collect();
559-
}
560-
561-
fn chars_cmp_with_unwrap() {
562-
let s = String::from("foo");
563-
if s.chars().next().unwrap() == 'f' { // s.starts_with('f')
564-
// Nothing here
565-
}
566-
if s.chars().next_back().unwrap() == 'o' { // s.ends_with('o')
567-
// Nothing here
568-
}
569-
if s.chars().last().unwrap() == 'o' { // s.ends_with('o')
570-
// Nothing here
571-
}
572-
if s.chars().next().unwrap() != 'f' { // !s.starts_with('f')
573-
// Nothing here
574-
}
575-
if s.chars().next_back().unwrap() != 'o' { // !s.ends_with('o')
576-
// Nothing here
577-
}
578-
if s.chars().last().unwrap() != 'o' { // !s.ends_with('o')
579-
// Nothing here
580-
}
581-
}
582-
583-
#[allow(unnecessary_operation)]
584-
fn ends_with() {
585-
"".chars().last() == Some(' ');
586-
Some(' ') != "".chars().last();
587-
"".chars().next_back() == Some(' ');
588-
Some(' ') != "".chars().next_back();
589356
}

0 commit comments

Comments
 (0)