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

Fix a clippy warning when using clone_from = "true" #77

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ script:
- cargo clean
- cargo build --verbose --features=use_core
- cargo test --verbose --features=use_core
- if ${HAS_CLIPPY}; then cargo clippy --verbose --features=use_core; fi
- if ${HAS_CLIPPY}; then cargo clippy --verbose --all-targets --features=use_core; fi

- if [ ${TRAVIS_RUST_VERSION} = "nightly" ]; then cargo update -Z minimal-versions; fi
- if [ ${TRAVIS_RUST_VERSION} = "nightly" ]; then cargo build --verbose; fi
- if [ ${TRAVIS_RUST_VERSION} = "nightly" ]; then cargo test --verbose; fi
- if [ ${TRAVIS_RUST_VERSION} = "nightly" ]; then cargo build --features=use_core; fi
- if [ ${TRAVIS_RUST_VERSION} = "nightly" ]; then cargo test --verbose --features=use_core; fi

- if [ ${TRAVIS_RUST_VERSION} = "stable" ]; then cargo test compile_test -- --ignored; fi
- if [ ${TRAVIS_RUST_VERSION} = "stable" ]; then cargo test compile_test -- --ignored; fi
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test_script:
- cargo test --verbose
- cargo clean
- cargo test --verbose --features=use_core
- if defined HAS_CLIPPY cargo clippy --verbose --features=use_core
- if defined HAS_CLIPPY cargo clippy --verbose --all-targets --features=use_core

- if "%CONFIGURATION%" == "nightly" cargo update -Z minimal-versions
- if "%CONFIGURATION%" == "nightly" cargo build --verbose
Expand Down
1 change: 1 addition & 0 deletions src/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub fn derive_clone(input: &ast::Input) -> proc_macro2::TokenStream {
};

quote! {
#[allow(clippy::needless_return)]
fn clone_from(&mut self, other: &Self) {
match *self {
#body
Expand Down
13 changes: 13 additions & 0 deletions tests/clippy-warning-clone-from.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![deny(clippy::all)]

#[cfg(feature = "use_core")]
extern crate core;

#[macro_use]
extern crate derivative;

#[derive(Derivative)]
#[derivative(Clone(clone_from = "true"))]
pub struct Foo {}

fn main() {}
12 changes: 7 additions & 5 deletions tests/derive-clone.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::blacklisted_name, clippy::redundant_clone)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -8,12 +10,12 @@ extern crate derivative;
#[derivative(Clone)]
struct Foo {
foo: u8,
#[derivative(Clone(clone_with="seventh"))]
#[derivative(Clone(clone_with = "seventh"))]
bar: u8,
}

fn seventh(a: &u8) -> u8 {
a/7
a / 7
}

#[derive(Debug, PartialEq)]
Expand All @@ -30,15 +32,15 @@ impl Clone for EvilCloneFrom {
}

#[derive(Derivative)]
#[derivative(Clone(clone_from="true"))]
#[derivative(Clone(clone_from = "true"))]
struct StructWithCloneFrom(EvilCloneFrom);

#[derive(Debug, Derivative, PartialEq)]
#[derivative(Clone(clone_from="true"))]
#[derivative(Clone(clone_from = "true"))]
enum EnumWithCloneFrom {
Evil(EvilCloneFrom),
Good(u32),
None
None,
}

#[test]
Expand Down
10 changes: 6 additions & 4 deletions tests/derive-eq.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::eq_op)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -7,14 +9,14 @@ extern crate derivative;
#[derive(Derivative, PartialEq)]
#[derivative(Eq)]
struct Foo {
foo: u8
foo: u8,
}

#[derive(Derivative)]
#[derivative(Eq)]
struct WithPtr<T: ?Sized> {
#[derivative(Eq(bound=""))]
foo: *const T
#[derivative(Eq(bound = ""))]
foo: *const T,
}

impl<T: ?Sized> PartialEq for WithPtr<T> {
Expand All @@ -26,7 +28,7 @@ impl<T: ?Sized> PartialEq for WithPtr<T> {
trait SomeTrait {}
struct SomeType {
#[allow(dead_code)]
foo: u8
foo: u8,
}
impl SomeTrait for SomeType {}

Expand Down
2 changes: 2 additions & 0 deletions tests/derive-partial-eq.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::eq_op)]

#[cfg(feature = "use_core")]
extern crate core;

Expand Down
21 changes: 11 additions & 10 deletions tests/rustc-class-implement-traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ trait noisy {
#[derive(Derivative)]
#[derivative(Clone)]
struct cat {
meows : usize,
meows: usize,

how_hungry : isize,
name : String,
how_hungry: isize,
name: String,
}

impl cat {
Expand All @@ -44,28 +44,29 @@ impl cat {
if self.how_hungry > 0 {
println!("OM NOM NOM");
self.how_hungry -= 2;
return true;
true
} else {
println!("Not hungry!");
return false;
false
}
}
}

impl noisy for cat {
fn speak(&mut self) { self.meow(); }
fn speak(&mut self) {
self.meow();
}
}

fn cat(in_x : usize, in_y : isize, in_name: String) -> cat {
fn cat(in_x: usize, in_y: isize, in_name: String) -> cat {
cat {
meows: in_x,
how_hungry: in_y,
name: in_name.clone()
name: in_name,
}
}


fn make_speak<C:noisy>(mut c: C) {
fn make_speak<C: noisy>(mut c: C) {
c.speak();
}

Expand Down
4 changes: 3 additions & 1 deletion tests/rustc-deriving-clone-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// pretty-expanded FIXME #23616

#![allow(clippy::redundant_clone)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -36,7 +38,7 @@ struct S {

_bool: bool,
_char: char,
_nil: ()
_nil: (),
}

#[test]
Expand Down
2 changes: 2 additions & 0 deletions tests/rustc-deriving-copyclone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//! Test that #[derive(Copy, Clone)] produces a shallow copy
//! even when a member violates RFC 1521

#![allow(clippy::clone_on_copy)]

#[cfg(feature = "use_core")]
extern crate core;

Expand Down
18 changes: 12 additions & 6 deletions tests/rustc-deriving-hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extern crate core;
#[macro_use]
extern crate derivative;

use std::hash::{Hash, Hasher};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

#[derive(Derivative)]
#[derivative(Hash)]
Expand All @@ -29,14 +29,20 @@ struct Person {

// test for hygiene name collisions
#[derive(Derivative)]
#[derivative(Hash)] struct __H__H;
#[derivative(Hash)]
struct __H__H;
#[derive(Derivative)]
#[allow(dead_code)] #[derivative(Hash)] struct Collision<__H> ( __H );
#[allow(dead_code)]
#[derivative(Hash)]
struct Collision<__H>(__H);
// TODO(rustc) #[derivative(Hash)] enum Collision<__H> { __H { __H__H: __H } }

#[derive(Derivative)]
#[derivative(Hash)]
enum E { A=1, B }
enum E {
A = 1,
B,
}

fn hash<T: Hash>(t: &T) -> u64 {
let mut s = DefaultHasher::new();
Expand Down Expand Up @@ -64,12 +70,12 @@ fn main() {
let person1 = Person {
id: 5,
name: "Janet".to_string(),
phone: 555_666_7777
phone: 555_666_777,
};
let person2 = Person {
id: 5,
name: "Bob".to_string(),
phone: 555_666_7777
phone: 555_666_777,
};
assert_eq!(hash(&person1), hash(&person1));
assert!(hash(&person1) != hash(&person2));
Expand Down
10 changes: 6 additions & 4 deletions tests/rustc-deriving-meta-multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// pretty-expanded FIXME #23616

#![allow(clippy::eq_op, clippy::redundant_clone)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -25,16 +27,16 @@ use std::hash::Hash;
#[derivative(Hash)]
struct Foo {
bar: usize,
baz: isize
baz: isize,
}

fn hash<T: Hash>(_t: &T) {}

#[test]
fn main() {
let a = Foo {bar: 4, baz: -3};
let a = Foo { bar: 4, baz: -3 };

let _ = a == a; // check for PartialEq impl w/o testing its correctness
let _ = a == a; // check for PartialEq impl w/o testing its correctness
let _ = a.clone(); // check for Clone impl w/o testing its correctness
hash(&a); // check for Hash impl w/o testing its correctness
hash(&a); // check for Hash impl w/o testing its correctness
}
10 changes: 6 additions & 4 deletions tests/rustc-deriving-meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

// pretty-expanded FIXME #23616

#![allow(clippy::eq_op, clippy::redundant_clone)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -22,16 +24,16 @@ use std::hash::Hash;
#[derivative(PartialEq, Clone, Hash)]
struct Foo {
bar: usize,
baz: isize
baz: isize,
}

fn hash<T: Hash>(_t: &T) {}

#[test]
fn main() {
let a = Foo {bar: 4, baz: -3};
let a = Foo { bar: 4, baz: -3 };

let _ = a == a; // check for PartialEq impl w/o testing its correctness
let _ = a == a; // check for PartialEq impl w/o testing its correctness
let _ = a.clone(); // check for Clone impl w/o testing its correctness
hash(&a); // check for Hash impl w/o testing its correctness
hash(&a); // check for Hash impl w/o testing its correctness
}
48 changes: 37 additions & 11 deletions tests/rustc-issue-12860.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(clippy::derive_hash_xor_eq)]

#[cfg(feature = "use_core")]
extern crate core;

Expand All @@ -22,42 +24,66 @@ extern crate derivative;
struct XYZ {
x: isize,
y: isize,
z: isize
z: isize,
}

#[test]
fn main() {
let mut connected = HashSet::new();
let mut border = HashSet::new();

let middle = XYZ{x: 0, y: 0, z: 0};
let middle = XYZ { x: 0, y: 0, z: 0 };
border.insert(middle);

while !border.is_empty() && connected.len() < 10000 {
let choice = *(border.iter().next().unwrap());
border.remove(&choice);
connected.insert(choice);

let cxp = XYZ{x: choice.x + 1, y: choice.y, z: choice.z};
let cxm = XYZ{x: choice.x - 1, y: choice.y, z: choice.z};
let cyp = XYZ{x: choice.x, y: choice.y + 1, z: choice.z};
let cym = XYZ{x: choice.x, y: choice.y - 1, z: choice.z};
let czp = XYZ{x: choice.x, y: choice.y, z: choice.z + 1};
let czm = XYZ{x: choice.x, y: choice.y, z: choice.z - 1};
let cxp = XYZ {
x: choice.x + 1,
y: choice.y,
z: choice.z,
};
let cxm = XYZ {
x: choice.x - 1,
y: choice.y,
z: choice.z,
};
let cyp = XYZ {
x: choice.x,
y: choice.y + 1,
z: choice.z,
};
let cym = XYZ {
x: choice.x,
y: choice.y - 1,
z: choice.z,
};
let czp = XYZ {
x: choice.x,
y: choice.y,
z: choice.z + 1,
};
let czm = XYZ {
x: choice.x,
y: choice.y,
z: choice.z - 1,
};

if !connected.contains(&cxp) {
border.insert(cxp);
}
if !connected.contains(&cxm){
if !connected.contains(&cxm) {
border.insert(cxm);
}
if !connected.contains(&cyp){
if !connected.contains(&cyp) {
border.insert(cyp);
}
if !connected.contains(&cym) {
border.insert(cym);
}
if !connected.contains(&czp){
if !connected.contains(&czp) {
border.insert(czp);
}
if !connected.contains(&czm) {
Expand Down
Loading