rust/tests/ui/cmp_owned.rs

90 lines
1.6 KiB
Rust
Raw Normal View History

2018-10-06 18:18:06 +02:00
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
2018-10-11 12:16:22 +02:00
2017-09-18 12:47:33 +02:00
2018-07-28 17:34:52 +02:00
#[warn(clippy::cmp_owned)]
#[allow(clippy::unnecessary_operation)]
2015-05-21 14:51:43 +02:00
fn main() {
fn with_to_string(x : &str) {
x != "foo".to_string();
2017-02-08 14:58:07 +01:00
"foo".to_string() != x;
}
2016-01-24 10:16:56 +01:00
let x = "oh";
with_to_string(x);
2017-02-08 14:58:07 +01:00
x != "foo".to_owned();
2017-02-08 14:58:07 +01:00
x != String::from("foo");
2016-01-18 15:35:50 +01:00
42.to_string() == "42";
2017-05-11 18:59:36 +02:00
Foo.to_owned() == Foo;
2018-10-09 04:04:29 +02:00
"abc".chars().filter(|c| c.to_owned() != 'X');
"abc".chars().filter(|c| *c != 'X');
let x = &Baz;
let y = &Baz;
y.to_owned() == *x;
let x = &&Baz;
let y = &Baz;
y.to_owned() == **x;
2017-05-11 18:59:36 +02:00
}
struct Foo;
impl PartialEq for Foo {
fn eq(&self, other: &Self) -> bool {
self.to_owned() == *other
}
}
impl ToOwned for Foo {
type Owned = Bar;
fn to_owned(&self) -> Bar {
Bar
}
}
#[derive(PartialEq)]
struct Bar;
impl PartialEq<Foo> for Bar {
fn eq(&self, _: &Foo) -> bool {
true
}
}
impl std::borrow::Borrow<Foo> for Bar {
fn borrow(&self) -> &Foo {
static FOO: Foo = Foo;
&FOO
}
2015-05-21 14:51:43 +02:00
}
#[derive(PartialEq)]
struct Baz;
impl ToOwned for Baz {
type Owned = Baz;
fn to_owned(&self) -> Baz {
Baz
}
}