Skip to content

Commit

Permalink
Auto merge of #3610 - phansch:method_rs_cleanup, r=flip1995
Browse files Browse the repository at this point in the history
UI test cleanup: Extract lint from methods.rs test

Extracts the `result_map_unwrap_or_else` lint into a separate test file.

This also extracts the `IteratorFalsePositives` struct and impl into
`auxiliary/option_helpers.rs`.

cc #2038
  • Loading branch information
bors committed Jan 5, 2019
2 parents cf3fb90 + a5d3f37 commit 3846b8b
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 169 deletions.
47 changes: 47 additions & 0 deletions tests/ui/auxiliary/option_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![allow(dead_code, unused_variables)]

/// Utility macro to test linting behavior in `option_methods()`
/// The lints included in `option_methods()` should not lint if the call to map is partially
/// within a macro
#[macro_export]
macro_rules! opt_map {
($opt:expr, $map:expr) => {
($opt).map($map)
};
}

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
pub struct IteratorFalsePositives {
pub foo: u32,
}

impl IteratorFalsePositives {
pub fn filter(self) -> IteratorFalsePositives {
self
}

pub fn next(self) -> IteratorFalsePositives {
self
}

pub fn find(self) -> Option<u32> {
Some(self.foo)
}

pub fn position(self) -> Option<u32> {
Some(self.foo)
}

pub fn rposition(self) -> Option<u32> {
Some(self.foo)
}

pub fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

pub fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}
38 changes: 4 additions & 34 deletions tests/ui/iter_skip_next.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:option_helpers.rs

#![warn(clippy::iter_skip_next)]
#![allow(clippy::blacklisted_name)]

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
struct IteratorFalsePositives {
foo: u32,
}

impl IteratorFalsePositives {
fn filter(self) -> IteratorFalsePositives {
self
}

fn next(self) -> IteratorFalsePositives {
self
}

fn find(self) -> Option<u32> {
Some(self.foo)
}
extern crate option_helpers;

fn position(self) -> Option<u32> {
Some(self.foo)
}

fn rposition(self) -> Option<u32> {
Some(self.foo)
}

fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}
use option_helpers::IteratorFalsePositives;

/// Checks implementation of `ITER_SKIP_NEXT` lint
fn iter_skip_next() {
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/iter_skip_next.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:52:13
--> $DIR/iter_skip_next.rs:22:13
|
LL | let _ = some_vec.iter().skip(42).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::iter-skip-next` implied by `-D warnings`

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:53:13
--> $DIR/iter_skip_next.rs:23:13
|
LL | let _ = some_vec.iter().cycle().skip(42).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:54:13
--> $DIR/iter_skip_next.rs:24:13
|
LL | let _ = (1..10).skip(10).next();
| ^^^^^^^^^^^^^^^^^^^^^^^

error: called `skip(x).next()` on an iterator. This is more succinctly expressed by calling `nth(x)`
--> $DIR/iter_skip_next.rs:55:14
--> $DIR/iter_skip_next.rs:25:14
|
LL | let _ = &some_vec[..].iter().skip(3).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
73 changes: 7 additions & 66 deletions tests/ui/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:option_helpers.rs

#![warn(clippy::all, clippy::pedantic, clippy::option_unwrap_used)]
#![allow(
clippy::blacklisted_name,
Expand All @@ -22,6 +24,9 @@
clippy::useless_format
)]

#[macro_use]
extern crate option_helpers;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::HashSet;
Expand All @@ -31,6 +36,8 @@ use std::iter::FromIterator;
use std::rc::{self, Rc};
use std::sync::{self, Arc};

use option_helpers::IteratorFalsePositives;

pub struct T;

impl T {
Expand Down Expand Up @@ -101,13 +108,6 @@ impl Mul<T> for T {
fn mul(self, other: T) -> T { self } // no error, obviously
}

/// Utility macro to test linting behavior in `option_methods()`
/// The lints included in `option_methods()` should not lint if the call to map is partially
/// within a macro
macro_rules! opt_map {
($opt:expr, $map:expr) => {($opt).map($map)};
}

/// Checks implementation of the following lints:
/// * `OPTION_MAP_UNWRAP_OR`
/// * `OPTION_MAP_UNWRAP_OR_ELSE`
Expand Down Expand Up @@ -169,29 +169,6 @@ fn option_methods() {
);
}

/// Checks implementation of the following lints:
/// * `RESULT_MAP_UNWRAP_OR_ELSE`
fn result_methods() {
let res: Result<i32, ()> = Ok(1);

// Check RESULT_MAP_UNWRAP_OR_ELSE
// single line case
let _ = res.map(|x| x + 1)

.unwrap_or_else(|e| 0); // should lint even though this call is on a separate line
// multi line cases
let _ = res.map(|x| {
x + 1
}
).unwrap_or_else(|e| 0);
let _ = res.map(|x| x + 1)
.unwrap_or_else(|e|
0
);
// macro case
let _ = opt_map!(res, |x| x + 1).unwrap_or_else(|e| 0); // should not lint
}

/// Struct to generate false positives for things with .iter()
#[derive(Copy, Clone)]
struct HasIter;
Expand All @@ -206,42 +183,6 @@ impl HasIter {
}
}

/// Struct to generate false positive for Iterator-based lints
#[derive(Copy, Clone)]
struct IteratorFalsePositives {
foo: u32,
}

impl IteratorFalsePositives {
fn filter(self) -> IteratorFalsePositives {
self
}

fn next(self) -> IteratorFalsePositives {
self
}

fn find(self) -> Option<u32> {
Some(self.foo)
}

fn position(self) -> Option<u32> {
Some(self.foo)
}

fn rposition(self) -> Option<u32> {
Some(self.foo)
}

fn nth(self, n: usize) -> Option<u32> {
Some(self.foo)
}

fn skip(self, _: usize) -> IteratorFalsePositives {
self
}
}

/// Checks implementation of `FILTER_NEXT` lint
fn filter_next() {
let v = vec![3, 2, 1, 0, -1, -2, -3];
Expand Down
Loading

0 comments on commit 3846b8b

Please sign in to comment.