mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 13:43:17 +00:00
af39a8a4a8
If the type of the result of a call to `IntoIterator::into_iter()` and the type of the receiver are the same, then the receiver implements `Iterator` and `into_iter()` is the identity function. The call to `into_iter()` may be removed in all but two cases: - If the receiver implements `Copy`, `into_iter()` will produce a copy of the receiver and cannot be removed. For example, `x.into_iter().next()` will not advance `x` while `x.next()` will. - If the receiver is an immutable local variable and the call to `into_iter()` appears in a larger expression, removing the call to `into_iter()` might cause mutability issues. For example, if `x` is an immutable local variable, `x.into_iter().next()` will compile while `x.next()` will not as `next()` receives `&mut self`.
159 lines
4 KiB
Rust
159 lines
4 KiB
Rust
// run-rustfix
|
|
|
|
#![deny(clippy::useless_conversion)]
|
|
#![allow(clippy::unnecessary_wraps)]
|
|
|
|
fn test_generic<T: Copy>(val: T) -> T {
|
|
let _ = val;
|
|
val
|
|
}
|
|
|
|
fn test_generic2<T: Copy + Into<i32> + Into<U>, U: From<T>>(val: T) {
|
|
// ok
|
|
let _: i32 = val.into();
|
|
let _: U = val.into();
|
|
let _ = U::from(val);
|
|
}
|
|
|
|
fn test_questionmark() -> Result<(), ()> {
|
|
{
|
|
let _: i32 = 0i32;
|
|
Ok(Ok(()))
|
|
}??;
|
|
Ok(())
|
|
}
|
|
|
|
fn test_issue_3913() -> Result<(), std::io::Error> {
|
|
use std::fs;
|
|
use std::path::Path;
|
|
|
|
let path = Path::new(".");
|
|
for _ in fs::read_dir(path)? {}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() {
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
let lines = text.lines();
|
|
if Some("ok") == lines.into_iter().next() {}
|
|
}
|
|
|
|
fn lint_into_iter_on_mutable_local_implementing_iterator_in_expr() {
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
let mut lines = text.lines();
|
|
if Some("ok") == lines.next() {}
|
|
}
|
|
|
|
fn lint_into_iter_on_expr_implementing_iterator() {
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
let mut lines = text.lines();
|
|
if Some("ok") == lines.next() {}
|
|
}
|
|
|
|
fn lint_into_iter_on_expr_implementing_iterator_2() {
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
if Some("ok") == text.lines().next() {}
|
|
}
|
|
|
|
#[allow(const_item_mutation)]
|
|
fn lint_into_iter_on_const_implementing_iterator() {
|
|
const NUMBERS: std::ops::Range<i32> = 0..10;
|
|
let _ = NUMBERS.next();
|
|
}
|
|
|
|
fn lint_into_iter_on_const_implementing_iterator_2() {
|
|
const NUMBERS: std::ops::Range<i32> = 0..10;
|
|
let mut n = NUMBERS;
|
|
n.next();
|
|
}
|
|
|
|
#[derive(Clone, Copy)]
|
|
struct CopiableCounter {
|
|
counter: u32,
|
|
}
|
|
|
|
impl Iterator for CopiableCounter {
|
|
type Item = u32;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
self.counter = self.counter.wrapping_add(1);
|
|
Some(self.counter)
|
|
}
|
|
}
|
|
|
|
fn dont_lint_into_iter_on_copy_iter() {
|
|
let mut c = CopiableCounter { counter: 0 };
|
|
assert_eq!(c.into_iter().next(), Some(1));
|
|
assert_eq!(c.into_iter().next(), Some(1));
|
|
assert_eq!(c.next(), Some(1));
|
|
assert_eq!(c.next(), Some(2));
|
|
}
|
|
|
|
fn dont_lint_into_iter_on_static_copy_iter() {
|
|
static mut C: CopiableCounter = CopiableCounter { counter: 0 };
|
|
unsafe {
|
|
assert_eq!(C.into_iter().next(), Some(1));
|
|
assert_eq!(C.into_iter().next(), Some(1));
|
|
assert_eq!(C.next(), Some(1));
|
|
assert_eq!(C.next(), Some(2));
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
test_generic(10i32);
|
|
test_generic2::<i32, i32>(10i32);
|
|
test_questionmark().unwrap();
|
|
test_issue_3913().unwrap();
|
|
|
|
dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr();
|
|
lint_into_iter_on_mutable_local_implementing_iterator_in_expr();
|
|
lint_into_iter_on_expr_implementing_iterator();
|
|
lint_into_iter_on_expr_implementing_iterator_2();
|
|
lint_into_iter_on_const_implementing_iterator();
|
|
lint_into_iter_on_const_implementing_iterator_2();
|
|
dont_lint_into_iter_on_copy_iter();
|
|
dont_lint_into_iter_on_static_copy_iter();
|
|
|
|
let _: String = "foo".into();
|
|
let _: String = From::from("foo");
|
|
let _ = String::from("foo");
|
|
#[allow(clippy::useless_conversion)]
|
|
{
|
|
let _: String = "foo".into();
|
|
let _ = String::from("foo");
|
|
let _ = "".lines().into_iter();
|
|
}
|
|
|
|
let _: String = "foo".to_string();
|
|
let _: String = "foo".to_string();
|
|
let _ = "foo".to_string();
|
|
let _ = format!("A: {:04}", 123);
|
|
let _ = "".lines();
|
|
let _ = vec![1, 2, 3].into_iter();
|
|
let _: String = format!("Hello {}", "world");
|
|
|
|
// keep parentheses around `a + b` for suggestion (see #4750)
|
|
let a: i32 = 1;
|
|
let b: i32 = 1;
|
|
let _ = (a + b) * 3;
|
|
|
|
// see #7205
|
|
let s: Foo<'a'> = Foo;
|
|
let _: Foo<'b'> = s.into();
|
|
let s2: Foo<'a'> = Foo;
|
|
let _: Foo<'a'> = s2;
|
|
let s3: Foo<'a'> = Foo;
|
|
let _ = s3;
|
|
let s4: Foo<'a'> = Foo;
|
|
let _ = vec![s4, s4, s4].into_iter();
|
|
}
|
|
|
|
#[derive(Copy, Clone)]
|
|
struct Foo<const C: char>;
|
|
|
|
impl From<Foo<'a'>> for Foo<'b'> {
|
|
fn from(_s: Foo<'a'>) -> Self {
|
|
Foo
|
|
}
|
|
}
|