2020-05-17 15:36:26 +00:00
|
|
|
#![deny(clippy::useless_conversion)]
|
2023-06-10 11:43:30 +00:00
|
|
|
#![allow(clippy::needless_if, clippy::unnecessary_wraps)]
|
2017-02-05 04:41:09 +00:00
|
|
|
|
|
|
|
fn test_generic<T: Copy>(val: T) -> T {
|
|
|
|
let _ = T::from(val);
|
|
|
|
val.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
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.into();
|
|
|
|
Ok(Ok(()))
|
|
|
|
}??;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-05-12 19:32:29 +00:00
|
|
|
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(())
|
|
|
|
}
|
|
|
|
|
2023-05-14 17:21:23 +00:00
|
|
|
fn dont_lint_on_type_alias() {
|
|
|
|
type A = i32;
|
|
|
|
_ = A::from(0i32);
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:58:28 +00:00
|
|
|
fn dont_lint_into_iter_on_immutable_local_implementing_iterator_in_expr() {
|
2020-07-26 19:07:07 +00:00
|
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
|
|
let lines = text.lines();
|
|
|
|
if Some("ok") == lines.into_iter().next() {}
|
2022-12-01 16:58:28 +00:00
|
|
|
}
|
2020-07-26 19:07:07 +00:00
|
|
|
|
2022-12-01 16:58:28 +00:00
|
|
|
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.into_iter().next() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_into_iter_on_expr_implementing_iterator() {
|
|
|
|
let text = "foo\r\nbar\n\nbaz\n";
|
|
|
|
let mut lines = text.lines().into_iter();
|
|
|
|
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().into_iter().next() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(const_item_mutation)]
|
|
|
|
fn lint_into_iter_on_const_implementing_iterator() {
|
|
|
|
const NUMBERS: std::ops::Range<i32> = 0..10;
|
|
|
|
let _ = NUMBERS.into_iter().next();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_into_iter_on_const_implementing_iterator_2() {
|
|
|
|
const NUMBERS: std::ops::Range<i32> = 0..10;
|
|
|
|
let mut n = NUMBERS.into_iter();
|
|
|
|
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));
|
|
|
|
}
|
2020-07-26 19:07:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 04:41:09 +00:00
|
|
|
fn main() {
|
|
|
|
test_generic(10i32);
|
|
|
|
test_generic2::<i32, i32>(10i32);
|
|
|
|
test_questionmark().unwrap();
|
2019-05-12 19:32:29 +00:00
|
|
|
test_issue_3913().unwrap();
|
2022-12-01 16:58:28 +00:00
|
|
|
|
2023-05-14 17:21:23 +00:00
|
|
|
dont_lint_on_type_alias();
|
2022-12-01 16:58:28 +00:00
|
|
|
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();
|
2017-02-05 04:41:09 +00:00
|
|
|
|
|
|
|
let _: String = "foo".into();
|
|
|
|
let _: String = From::from("foo");
|
|
|
|
let _ = String::from("foo");
|
2020-05-17 15:36:26 +00:00
|
|
|
#[allow(clippy::useless_conversion)]
|
2017-10-04 13:26:41 +00:00
|
|
|
{
|
|
|
|
let _: String = "foo".into();
|
|
|
|
let _ = String::from("foo");
|
2018-07-30 04:02:05 +00:00
|
|
|
let _ = "".lines().into_iter();
|
2017-10-04 13:26:41 +00:00
|
|
|
}
|
2017-02-05 04:41:09 +00:00
|
|
|
|
|
|
|
let _: String = "foo".to_string().into();
|
|
|
|
let _: String = From::from("foo".to_string());
|
|
|
|
let _ = String::from("foo".to_string());
|
2018-08-07 03:37:11 +00:00
|
|
|
let _ = String::from(format!("A: {:04}", 123));
|
2018-07-30 04:02:05 +00:00
|
|
|
let _ = "".lines().into_iter();
|
|
|
|
let _ = vec![1, 2, 3].into_iter().into_iter();
|
2018-10-27 09:01:27 +00:00
|
|
|
let _: String = format!("Hello {}", "world").into();
|
2020-08-28 14:10:16 +00:00
|
|
|
|
2021-10-17 10:04:01 +00:00
|
|
|
// keep parentheses around `a + b` for suggestion (see #4750)
|
2020-08-28 14:10:16 +00:00
|
|
|
let a: i32 = 1;
|
|
|
|
let b: i32 = 1;
|
|
|
|
let _ = i32::from(a + b) * 3;
|
2021-05-20 10:30:31 +00:00
|
|
|
|
|
|
|
// see #7205
|
|
|
|
let s: Foo<'a'> = Foo;
|
|
|
|
let _: Foo<'b'> = s.into();
|
|
|
|
let s2: Foo<'a'> = Foo;
|
|
|
|
let _: Foo<'a'> = s2.into();
|
|
|
|
let s3: Foo<'a'> = Foo;
|
|
|
|
let _ = Foo::<'a'>::from(s3);
|
|
|
|
let s4: Foo<'a'> = Foo;
|
|
|
|
let _ = vec![s4, s4, s4].into_iter().into_iter();
|
|
|
|
}
|
|
|
|
|
2023-07-09 20:32:58 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn issue11065_fp() {
|
|
|
|
use std::option::IntoIter;
|
|
|
|
fn takes_into_iter(_: impl IntoIterator<Item = i32>) {}
|
|
|
|
|
|
|
|
macro_rules! x {
|
|
|
|
($e:expr) => {
|
|
|
|
takes_into_iter($e);
|
|
|
|
let _: IntoIter<i32> = $e; // removing `.into_iter()` leads to a type error here
|
|
|
|
};
|
|
|
|
}
|
|
|
|
x!(Some(5).into_iter());
|
|
|
|
}
|
|
|
|
|
2023-05-30 20:32:26 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
fn explicit_into_iter_fn_arg() {
|
|
|
|
fn a<T>(_: T) {}
|
|
|
|
fn b<T: IntoIterator<Item = i32>>(_: T) {}
|
|
|
|
fn c(_: impl IntoIterator<Item = i32>) {}
|
|
|
|
fn d<T>(_: T)
|
|
|
|
where
|
|
|
|
T: IntoIterator<Item = i32>,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
fn f(_: std::vec::IntoIter<i32>) {}
|
|
|
|
|
|
|
|
a(vec![1, 2].into_iter());
|
|
|
|
b(vec![1, 2].into_iter());
|
|
|
|
c(vec![1, 2].into_iter());
|
|
|
|
d(vec![1, 2].into_iter());
|
|
|
|
b([&1, &2, &3].into_iter().cloned());
|
|
|
|
|
|
|
|
b(vec![1, 2].into_iter().into_iter());
|
|
|
|
b(vec![1, 2].into_iter().into_iter().into_iter());
|
|
|
|
|
|
|
|
macro_rules! macro_generated {
|
|
|
|
() => {
|
|
|
|
vec![1, 2].into_iter()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
b(macro_generated!());
|
|
|
|
}
|
|
|
|
|
2021-05-20 10:30:31 +00:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct Foo<const C: char>;
|
|
|
|
|
|
|
|
impl From<Foo<'a'>> for Foo<'b'> {
|
|
|
|
fn from(_s: Foo<'a'>) -> Self {
|
|
|
|
Foo
|
|
|
|
}
|
2017-02-05 04:41:09 +00:00
|
|
|
}
|