Skip to content

Commit 3c8f768

Browse files
committed
Add clippy feature
1 parent 98f4cb4 commit 3c8f768

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ matrix:
55
- rust: beta
66
env: FEATURES="serde_impl"
77
- rust: nightly
8-
env: FEATURES="serde_impl nightly"
8+
env: FEATURES="serde_impl nightly clippy"
99
script:
1010
- cargo build --features "$FEATURES"
1111
- cargo test --features "$FEATURES"

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ nightly = []
2020
serde_impl = ["serde", "serde_json"]
2121

2222
[dependencies]
23+
clippy = { version = "0.*", optional = true }
2324
serde = { version = "^0.7", optional = true }
2425
serde_json = { version = "^0.7", optional = true }

src/lib.rs

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! A HashMap wrapper that holds key-value pairs in insertion order.
11+
//! A `HashMap` wrapper that holds key-value pairs in insertion order.
1212
//!
1313
//! # Examples
1414
//!
@@ -31,6 +31,10 @@
3131
#![cfg_attr(feature = "nightly", feature(hashmap_public_hasher))]
3232
#![cfg_attr(all(feature = "nightly", test), feature(test))]
3333

34+
#![cfg_attr(feature = "clippy", feature(plugin))]
35+
#![cfg_attr(feature = "clippy", plugin(clippy))]
36+
#![cfg_attr(feature = "clippy", deny(clippy))]
37+
3438
// Optional Serde support
3539
#[cfg(feature = "serde_impl")]
3640
mod serde;
@@ -303,7 +307,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
303307
self.detach(node_ptr);
304308
self.attach(node_ptr);
305309
}
306-
return value;
310+
value
307311
}
308312

309313
/// Removes and returns the value corresponding to the key from the map.
@@ -368,14 +372,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
368372
/// ```
369373
#[inline]
370374
pub fn pop_front(&mut self) -> Option<(K, V)> {
371-
if self.len() > 0 {
372-
let lru = unsafe { (*self.head).prev };
373-
self.detach(lru);
374-
return self.map
375-
.remove(&KeyRef{k: unsafe { &(*lru).key }})
376-
.map(|e| { let e = *e; (e.key, e.value) })
375+
if self.is_empty() {
376+
return None
377377
}
378-
None
378+
let lru = unsafe { (*self.head).prev };
379+
self.detach(lru);
380+
self.map
381+
.remove(&KeyRef{k: unsafe { &(*lru).key }})
382+
.map(|e| { let e = *e; (e.key, e.value) })
379383
}
380384

381385
/// Gets the first entry.
@@ -391,12 +395,12 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
391395
/// ```
392396
#[inline]
393397
pub fn front(&self) -> Option<(&K, &V)> {
394-
if self.len() > 0 {
395-
let lru = unsafe { (*self.head).prev };
396-
return self.map.get(&KeyRef{k: unsafe { &(*lru).key }})
397-
.map(|e| (&e.key, &e.value))
398+
if self.is_empty() {
399+
return None
398400
}
399-
None
401+
let lru = unsafe { (*self.head).prev };
402+
self.map.get(&KeyRef{k: unsafe { &(*lru).key }})
403+
.map(|e| (&e.key, &e.value))
400404
}
401405

402406
/// Removes the last entry.
@@ -414,14 +418,14 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
414418
/// ```
415419
#[inline]
416420
pub fn pop_back(&mut self) -> Option<(K, V)> {
417-
if self.len() > 0 {
418-
let mru = unsafe { (*self.head).next };
419-
self.detach(mru);
420-
return self.map
421-
.remove(&KeyRef{k: unsafe { &(*mru).key }})
422-
.map(|e| { let e = *e; (e.key, e.value) })
421+
if self.is_empty() {
422+
return None
423423
}
424-
None
424+
let mru = unsafe { (*self.head).next };
425+
self.detach(mru);
426+
self.map
427+
.remove(&KeyRef{k: unsafe { &(*mru).key }})
428+
.map(|e| { let e = *e; (e.key, e.value) })
425429
}
426430

427431
/// Gets the last entry.
@@ -437,12 +441,12 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
437441
/// ```
438442
#[inline]
439443
pub fn back(&mut self) -> Option<(&K, &V)> {
440-
if self.len() > 0 {
441-
let mru = unsafe { (*self.head).next };
442-
return self.map.get(&KeyRef{k: unsafe { &(*mru).key }})
443-
.map(|e| (&e.key, &e.value))
444+
if self.is_empty() {
445+
return None
444446
}
445-
None
447+
let mru = unsafe { (*self.head).next };
448+
self.map.get(&KeyRef{k: unsafe { &(*mru).key }})
449+
.map(|e| (&e.key, &e.value))
446450
}
447451

448452
/// Returns the number of key-value pairs in the map.
@@ -482,10 +486,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
482486
/// assert_eq!(None, iter.next());
483487
/// ```
484488
pub fn iter(&self) -> Iter<K, V> {
485-
let head = if ! self.head.is_null() {
486-
unsafe { (*self.head).prev }
487-
} else {
489+
let head = if self.head.is_null() {
488490
ptr::null_mut()
491+
} else {
492+
unsafe { (*self.head).prev }
489493
};
490494
Iter {
491495
head: head,
@@ -516,10 +520,10 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
516520
/// assert_eq!(&17, map.get(&"a").unwrap());
517521
/// ```
518522
pub fn iter_mut(&mut self) -> IterMut<K, V> {
519-
let head = if ! self.head.is_null() {
520-
unsafe { (*self.head).prev }
521-
} else {
523+
let head = if self.head.is_null() {
522524
ptr::null_mut()
525+
} else {
526+
unsafe { (*self.head).prev }
523527
};
524528
IterMut {
525529
head: head,
@@ -546,6 +550,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
546550
/// assert_eq!(&'b', keys.next().unwrap());
547551
/// assert_eq!(None, keys.next());
548552
/// ```
553+
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))] // false positive
549554
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
550555
fn first<A, B>((a, _): (A, B)) -> A { a }
551556
let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn ptr
@@ -570,6 +575,7 @@ impl<K: Hash + Eq, V, S: BuildHasher> LinkedHashMap<K, V, S> {
570575
/// assert_eq!(&20, values.next().unwrap());
571576
/// assert_eq!(None, values.next());
572577
/// ```
578+
#[cfg_attr(feature = "clippy", allow(needless_lifetimes))] // false positive
573579
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
574580
fn second<A, B>((_, b): (A, B)) -> B { b }
575581
let second: fn((&'a K, &'a V)) -> &'a V = second; // coerce to fn ptr
@@ -809,8 +815,7 @@ impl<'a, K, V> DoubleEndedIterator for Iter<'a, K, V> {
809815
self.remaining -= 1;
810816
unsafe {
811817
self.tail = (*self.tail).next;
812-
let r = Some((&(*self.tail).key, &(*self.tail).value));
813-
r
818+
Some((&(*self.tail).key, &(*self.tail).value))
814819
}
815820
}
816821
}
@@ -824,8 +829,7 @@ impl<'a, K, V> DoubleEndedIterator for IterMut<'a, K, V> {
824829
self.remaining -= 1;
825830
unsafe {
826831
self.tail = (*self.tail).next;
827-
let r = Some((&(*self.tail).key, &mut (*self.tail).value));
828-
r
832+
Some((&(*self.tail).key, &mut (*self.tail).value))
829833
}
830834
}
831835
}
@@ -841,6 +845,7 @@ impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
841845

842846
/// An insertion-order iterator over a `LinkedHashMap`'s keys.
843847
pub struct Keys<'a, K: 'a, V: 'a> {
848+
#[cfg_attr(feature = "clippy", allow(type_complexity))]
844849
inner: iter::Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
845850
}
846851

@@ -865,6 +870,7 @@ impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
865870

866871
/// An insertion-order iterator over a `LinkedHashMap`'s values.
867872
pub struct Values<'a, K: 'a, V: 'a> {
873+
#[cfg_attr(feature = "clippy", allow(type_complexity))]
868874
inner: iter::Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
869875
}
870876

0 commit comments

Comments
 (0)