Skip to content
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

Split some UI tests #2126

Merged
merged 7 commits into from
Oct 10, 2017
Merged
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
8 changes: 8 additions & 0 deletions tests/ui/cstring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn main() {}

#[allow(result_unwrap_used)]
fn temporary_cstring() {
use std::ffi::CString;

CString::new("foo").unwrap().as_ptr();
}
22 changes: 22 additions & 0 deletions tests/ui/cstring.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error: function is never used: `temporary_cstring`
--> $DIR/cstring.rs:4:1
|
4 | fn temporary_cstring() {
| ^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D dead-code` implied by `-D warnings`

error: you are getting the inner pointer of a temporary `CString`
--> $DIR/cstring.rs:7:5
|
7 | CString::new("foo").unwrap().as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D temporary-cstring-as-ptr` implied by `-D warnings`
= note: that pointer will be invalid outside this expression
help: assign the `CString` to a variable to extend its lifetime
--> $DIR/cstring.rs:7:5
|
7 | CString::new("foo").unwrap().as_ptr();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

46 changes: 46 additions & 0 deletions tests/ui/get_unwrap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![allow(unused_mut)]

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::iter::FromIterator;

struct GetFalsePositive {
arr: [u32; 3],
}

impl GetFalsePositive {
fn get(&self, pos: usize) -> Option<&u32> { self.arr.get(pos) }
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { self.arr.get_mut(pos) }
}

fn main() {
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
let mut some_slice = &mut [0, 1, 2, 3];
let mut some_vec = vec![0, 1, 2, 3];
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };

{ // Test `get().unwrap()`
let _ = boxed_slice.get(1).unwrap();
let _ = some_slice.get(0).unwrap();
let _ = some_vec.get(0).unwrap();
let _ = some_vecdeque.get(0).unwrap();
let _ = some_hashmap.get(&1).unwrap();
let _ = some_btreemap.get(&1).unwrap();
let _ = false_positive.get(0).unwrap();
}

{ // Test `get_mut().unwrap()`
*boxed_slice.get_mut(0).unwrap() = 1;
*some_slice.get_mut(0).unwrap() = 1;
*some_vec.get_mut(0).unwrap() = 1;
*some_vecdeque.get_mut(0).unwrap() = 1;
// Check false positives
*some_hashmap.get_mut(&1).unwrap() = 'b';
*some_btreemap.get_mut(&1).unwrap() = 'b';
*false_positive.get_mut(0).unwrap() = 1;
}
}
62 changes: 62 additions & 0 deletions tests/ui/get_unwrap.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:27:17
|
27 | let _ = boxed_slice.get(1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&boxed_slice[1]`
|
= note: `-D get-unwrap` implied by `-D warnings`

error: called `.get().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:28:17
|
28 | let _ = some_slice.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_slice[0]`

error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:29:17
|
29 | let _ = some_vec.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vec[0]`

error: called `.get().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:30:17
|
30 | let _ = some_vecdeque.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_vecdeque[0]`

error: called `.get().unwrap()` on a HashMap. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:31:17
|
31 | let _ = some_hashmap.get(&1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_hashmap[&1]`

error: called `.get().unwrap()` on a BTreeMap. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:32:17
|
32 | let _ = some_btreemap.get(&1).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&some_btreemap[&1]`

error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:37:10
|
37 | *boxed_slice.get_mut(0).unwrap() = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut boxed_slice[0]`

error: called `.get_mut().unwrap()` on a slice. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:38:10
|
38 | *some_slice.get_mut(0).unwrap() = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_slice[0]`

error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:39:10
|
39 | *some_vec.get_mut(0).unwrap() = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vec[0]`

error: called `.get_mut().unwrap()` on a VecDeque. Using `[]` is more clear and more concise
--> $DIR/get_unwrap.rs:40:10
|
40 | *some_vecdeque.get_mut(0).unwrap() = 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut some_vecdeque[0]`

233 changes: 0 additions & 233 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,6 @@ impl IteratorFalsePositives {
}
}

#[derive(Copy, Clone)]
struct HasChars;

impl HasChars {
fn chars(self) -> std::str::Chars<'static> {
"HasChars".chars()
}
}

/// Checks implementation of `FILTER_NEXT` lint
fn filter_next() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
Expand Down Expand Up @@ -358,232 +349,8 @@ fn iter_skip_next() {
let _ = foo.filter().skip(42).next();
}

struct GetFalsePositive {
arr: [u32; 3],
}

impl GetFalsePositive {
fn get(&self, pos: usize) -> Option<&u32> { self.arr.get(pos) }
fn get_mut(&mut self, pos: usize) -> Option<&mut u32> { self.arr.get_mut(pos) }
}

/// Checks implementation of `GET_UNWRAP` lint
fn get_unwrap() {
let mut boxed_slice: Box<[u8]> = Box::new([0, 1, 2, 3]);
let mut some_slice = &mut [0, 1, 2, 3];
let mut some_vec = vec![0, 1, 2, 3];
let mut some_vecdeque: VecDeque<_> = some_vec.iter().cloned().collect();
let mut some_hashmap: HashMap<u8, char> = HashMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut some_btreemap: BTreeMap<u8, char> = BTreeMap::from_iter(vec![(1, 'a'), (2, 'b')]);
let mut false_positive = GetFalsePositive { arr: [0, 1, 2] };

{ // Test `get().unwrap()`
let _ = boxed_slice.get(1).unwrap();
let _ = some_slice.get(0).unwrap();
let _ = some_vec.get(0).unwrap();
let _ = some_vecdeque.get(0).unwrap();
let _ = some_hashmap.get(&1).unwrap();
let _ = some_btreemap.get(&1).unwrap();
let _ = false_positive.get(0).unwrap();
}

{ // Test `get_mut().unwrap()`
*boxed_slice.get_mut(0).unwrap() = 1;
*some_slice.get_mut(0).unwrap() = 1;
*some_vec.get_mut(0).unwrap() = 1;
*some_vecdeque.get_mut(0).unwrap() = 1;
// Check false positives
*some_hashmap.get_mut(&1).unwrap() = 'b';
*some_btreemap.get_mut(&1).unwrap() = 'b';
*false_positive.get_mut(0).unwrap() = 1;
}
}


#[allow(similar_names)]
fn main() {
use std::io;

let opt = Some(0);
let _ = opt.unwrap();

let res: Result<i32, ()> = Ok(0);
let _ = res.unwrap();

res.ok().expect("disaster!");
// the following should not warn, since `expect` isn't implemented unless
// the error type implements `Debug`
let res2: Result<i32, MyError> = Ok(0);
res2.ok().expect("oh noes!");
let res3: Result<u32, MyErrorWithParam<u8>>= Ok(0);
res3.ok().expect("whoof");
let res4: Result<u32, io::Error> = Ok(0);
res4.ok().expect("argh");
let res5: io::Result<u32> = Ok(0);
res5.ok().expect("oops");
let res6: Result<u32, &str> = Ok(0);
res6.ok().expect("meh");
}

struct MyError(()); // doesn't implement Debug

#[derive(Debug)]
struct MyErrorWithParam<T> {
x: T
}

#[allow(unnecessary_operation)]
fn starts_with() {
"".chars().next() == Some(' ');
Some(' ') != "".chars().next();
}

fn str_extend_chars() {
let abc = "abc";
let def = String::from("def");
let mut s = String::new();

s.push_str(abc);
s.extend(abc.chars());

s.push_str("abc");
s.extend("abc".chars());

s.push_str(&def);
s.extend(def.chars());

s.extend(abc.chars().skip(1));
s.extend("abc".chars().skip(1));
s.extend(['a', 'b', 'c'].iter());

let f = HasChars;
s.extend(f.chars());
}

fn clone_on_copy() {
42.clone();

vec![1].clone(); // ok, not a Copy type
Some(vec![1]).clone(); // ok, not a Copy type
(&42).clone();
}

fn clone_on_ref_ptr() {
let rc = Rc::new(true);
let arc = Arc::new(true);

let rcweak = Rc::downgrade(&rc);
let arc_weak = Arc::downgrade(&arc);

rc.clone();
Rc::clone(&rc);

arc.clone();
Arc::clone(&arc);

rcweak.clone();
rc::Weak::clone(&rcweak);

arc_weak.clone();
sync::Weak::clone(&arc_weak);


}

fn clone_on_copy_generic<T: Copy>(t: T) {
t.clone();

Some(t).clone();
}

fn clone_on_double_ref() {
let x = vec![1];
let y = &&x;
let z: &Vec<_> = y.clone();

println!("{:p} {:p}",*y, z);
}

fn single_char_pattern() {
let x = "foo";
x.split("x");
x.split("xx");
x.split('x');

let y = "x";
x.split(y);
// Not yet testing for multi-byte characters
// Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern`
// should have done this but produced an ICE
//
// We may not want to suggest changing these anyway
// See: https://github.com/rust-lang-nursery/rust-clippy/issues/650#issuecomment-184328984
x.split("ß");
x.split("ℝ");
x.split("💣");
// Can't use this lint for unicode code points which don't fit in a char
x.split("❤️");
x.contains("x");
x.starts_with("x");
x.ends_with("x");
x.find("x");
x.rfind("x");
x.rsplit("x");
x.split_terminator("x");
x.rsplit_terminator("x");
x.splitn(0, "x");
x.rsplitn(0, "x");
x.matches("x");
x.rmatches("x");
x.match_indices("x");
x.rmatch_indices("x");
x.trim_left_matches("x");
x.trim_right_matches("x");

let h = HashSet::<String>::new();
h.contains("X"); // should not warn
}

#[allow(result_unwrap_used)]
fn temporary_cstring() {
use std::ffi::CString;

CString::new("foo").unwrap().as_ptr();
}

fn iter_clone_collect() {
let v = [1,2,3,4,5];
let v2 : Vec<isize> = v.iter().cloned().collect();
let v3 : HashSet<isize> = v.iter().cloned().collect();
let v4 : VecDeque<isize> = v.iter().cloned().collect();
}

fn chars_cmp_with_unwrap() {
let s = String::from("foo");
if s.chars().next().unwrap() == 'f' { // s.starts_with('f')
// Nothing here
}
if s.chars().next_back().unwrap() == 'o' { // s.ends_with('o')
// Nothing here
}
if s.chars().last().unwrap() == 'o' { // s.ends_with('o')
// Nothing here
}
if s.chars().next().unwrap() != 'f' { // !s.starts_with('f')
// Nothing here
}
if s.chars().next_back().unwrap() != 'o' { // !s.ends_with('o')
// Nothing here
}
if s.chars().last().unwrap() != 'o' { // !s.ends_with('o')
// Nothing here
}
}

#[allow(unnecessary_operation)]
fn ends_with() {
"".chars().last() == Some(' ');
Some(' ') != "".chars().last();
"".chars().next_back() == Some(' ');
Some(' ') != "".chars().next_back();
}
Loading