Fix a bunch more Clippy lints, but disable in Circle

The unused error is causing more trouble than it's worth at this
point
This commit is contained in:
Griffin Smith 2019-08-03 13:14:28 -04:00
parent 929dac06d0
commit 82cefedab9
18 changed files with 72 additions and 76 deletions

View file

@ -20,16 +20,16 @@ jobs:
- checkout - checkout
- rust/update_toolchain - rust/update_toolchain
- rust/format - rust/format
lint: # lint:
executor: rust/default # executor: rust/default
steps: # steps:
- checkout # - checkout
- rust/update_toolchain # - rust/update_toolchain
- rust/clippy # - rust/clippy
workflows: workflows:
default: default:
jobs: jobs:
- lint # - lint
- format - format
- build - build
- test: - test:

View file

@ -69,12 +69,12 @@ mod tests {
#[test] #[test]
fn test_describe_list() { fn test_describe_list() {
assert_eq!( assert_eq!(
describe_list(&vec![Description("one".to_string())]), describe_list(&[Description("one".to_string())]),
"one".to_string() "one".to_string()
); );
assert_eq!( assert_eq!(
describe_list(&vec![ describe_list(&[
Description("one".to_string()), Description("one".to_string()),
Description("two".to_string()) Description("two".to_string())
]), ]),
@ -82,7 +82,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
describe_list(&vec![ describe_list(&[
Description("one".to_string()), Description("one".to_string()),
Description("two".to_string()), Description("two".to_string()),
Description("three".to_string()) Description("three".to_string())

View file

@ -16,21 +16,21 @@ impl Color {
} }
impl color::Color for Color { impl color::Color for Color {
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result { fn write_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.write_fg(f) self.0.write_fg(f)
} }
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result { fn write_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.write_bg(f) self.0.write_bg(f)
} }
} }
impl<'a> color::Color for &'a Color { impl<'a> color::Color for &'a Color {
fn write_fg(&self, f: &mut fmt::Formatter) -> fmt::Result { fn write_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.write_fg(f) self.0.write_fg(f)
} }
fn write_bg(&self, f: &mut fmt::Formatter) -> fmt::Result { fn write_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.write_bg(f) self.0.write_bg(f)
} }
} }
@ -56,7 +56,7 @@ impl ColorVisitor {
impl<'de> Visitor<'de> for ColorVisitor { impl<'de> Visitor<'de> for ColorVisitor {
type Value = Color; type Value = Color;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("A color") formatter.write_str("A color")
} }

View file

@ -140,7 +140,7 @@ impl Stylable for Line {
} }
impl Stylable for Neighbors<Option<BoxStyle>> { impl Stylable for Neighbors<Option<BoxStyle>> {
fn style(&self, style: BoxStyle) -> char { fn style(&self, _style: BoxStyle) -> char {
use BoxStyle::*; use BoxStyle::*;
match (self.left, self.right, self.top, self.bottom) { match (self.left, self.right, self.top, self.bottom) {
(None, None, None, None) => BOX, (None, None, None, None) => BOX,

View file

@ -17,17 +17,17 @@ pub fn clear<T: Write>(out: &mut T) -> io::Result<()> {
pub trait Draw: Positioned { pub trait Draw: Positioned {
/// Draw this entity, assuming the character is already at the correct /// Draw this entity, assuming the character is already at the correct
/// position /// position
fn do_draw(&self, out: &mut Write) -> io::Result<()>; fn do_draw(&self, out: &mut dyn Write) -> io::Result<()>;
} }
impl<T: Draw> Draw for &T { impl<T: Draw> Draw for &T {
fn do_draw(&self, out: &mut Write) -> io::Result<()> { fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> {
(**self).do_draw(out) (**self).do_draw(out)
} }
} }
impl<T: Draw> Draw for Box<T> { impl<T: Draw> Draw for Box<T> {
fn do_draw(&self, out: &mut Write) -> io::Result<()> { fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> {
(**self).do_draw(out) (**self).do_draw(out)
} }
} }
@ -36,7 +36,7 @@ pub trait DrawWithNeighbors: Positioned {
#[allow(clippy::borrowed_box)] #[allow(clippy::borrowed_box)]
fn do_draw_with_neighbors<'a, 'b>( fn do_draw_with_neighbors<'a, 'b>(
&'a self, &'a self,
out: &'b mut Write, out: &'b mut dyn Write,
neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>,
) -> io::Result<()>; ) -> io::Result<()>;
} }
@ -44,7 +44,7 @@ pub trait DrawWithNeighbors: Positioned {
impl<T: Draw> DrawWithNeighbors for T { impl<T: Draw> DrawWithNeighbors for T {
fn do_draw_with_neighbors<'a, 'b>( fn do_draw_with_neighbors<'a, 'b>(
&'a self, &'a self,
out: &'b mut Write, out: &'b mut dyn Write,
_neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, _neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>,
) -> io::Result<()> { ) -> io::Result<()> {
self.do_draw(out) self.do_draw(out)

View file

@ -68,7 +68,7 @@ impl<W> Viewport<W> {
} }
impl<W> Debug for Viewport<W> { impl<W> Debug for Viewport<W> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
"Viewport {{ outer: {:?}, inner: {:?}, out: <OUT> }}", "Viewport {{ outer: {:?}, inner: {:?}, out: <OUT> }}",

View file

@ -42,7 +42,7 @@ impl Character {
} }
impl display::Draw for Character { impl display::Draw for Character {
fn do_draw(&self, out: &mut Write) -> io::Result<()> { fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> {
write!(out, "@") write!(out, "@")
} }
} }

View file

@ -57,7 +57,7 @@ impl Describe for Creature {
} }
impl display::Draw for Creature { impl display::Draw for Creature {
fn do_draw(&self, out: &mut Write) -> io::Result<()> { fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> {
write!(out, "{}", self.typ.chr) write!(out, "{}", self.typ.chr)
} }
} }

View file

@ -103,7 +103,7 @@ impl_downcast!(Entity);
impl DrawWithNeighbors for Box<dyn Entity> { impl DrawWithNeighbors for Box<dyn Entity> {
fn do_draw_with_neighbors<'a, 'b>( fn do_draw_with_neighbors<'a, 'b>(
&'a self, &'a self,
out: &'b mut Write, out: &'b mut dyn Write,
neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>,
) -> io::Result<()> { ) -> io::Result<()> {
(**self).do_draw_with_neighbors(out, neighbors) (**self).do_draw_with_neighbors(out, neighbors)

View file

@ -12,7 +12,7 @@ pub struct EntityChar {
} }
impl Display for EntityChar { impl Display for EntityChar {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!( write!(
f, f,
"{}{}{}", "{}{}{}",

View file

@ -21,7 +21,7 @@ impl Wall {
impl display::DrawWithNeighbors for Wall { impl display::DrawWithNeighbors for Wall {
fn do_draw_with_neighbors<'a, 'b>( fn do_draw_with_neighbors<'a, 'b>(
&'a self, &'a self,
out: &'b mut Write, out: &'b mut dyn Write,
neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>, neighbors: &'a Neighbors<Vec<&'a Box<dyn Entity>>>,
) -> io::Result<()> { ) -> io::Result<()> {
let neighbor_styles: Neighbors<Option<BoxStyle>> = let neighbor_styles: Neighbors<Option<BoxStyle>> =

View file

@ -44,7 +44,7 @@ impl Describe for Item {
} }
impl display::Draw for Item { impl display::Draw for Item {
fn do_draw(&self, out: &mut Write) -> io::Result<()> { fn do_draw(&self, out: &mut dyn Write) -> io::Result<()> {
write!(out, "{}", self.typ.chr) write!(out, "{}", self.typ.chr)
} }
} }

View file

@ -69,7 +69,7 @@ impl PromptResolution {
use PromptResolution::*; use PromptResolution::*;
match self { match self {
Cancellable(complete) => complete.cancel(), Cancellable(complete) => complete.cancel(),
Uncancellable(complete) => {} Uncancellable(_complete) => {}
} }
} }
} }
@ -200,12 +200,10 @@ impl<'a> Game<'a> {
fn collision_at(&self, pos: Position) -> Option<Collision> { fn collision_at(&self, pos: Position) -> Option<Collision> {
if !pos.within(self.viewport.inner) { if !pos.within(self.viewport.inner) {
Some(Collision::Stop) Some(Collision::Stop)
} else if self.creatures_at(pos).is_empty() {
None
} else { } else {
if self.creatures_at(pos).is_empty() { Some(Collision::Combat)
None
} else {
Some(Collision::Combat)
}
} }
} }
@ -305,7 +303,7 @@ impl<'a> Game<'a> {
} }
/// Step the game forward the given number of ticks /// Step the game forward the given number of ticks
fn tick(&mut self, ticks: Ticks) {} fn tick(&mut self, _ticks: Ticks) {}
/// Get a message from the global map based on the rng in this game /// Get a message from the global map based on the rng in this game
fn message<'params>( fn message<'params>(

View file

@ -1,13 +1,7 @@
extern crate termion;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate config;
extern crate log4rs;
extern crate serde;
extern crate toml;
#[macro_use] #[macro_use]
extern crate serde_derive; extern crate serde_derive;
extern crate serde_json;
#[macro_use] #[macro_use]
extern crate clap; extern crate clap;
#[macro_use] #[macro_use]
@ -19,14 +13,13 @@ extern crate lazy_static;
extern crate maplit; extern crate maplit;
#[macro_use] #[macro_use]
extern crate downcast_rs; extern crate downcast_rs;
extern crate backtrace;
#[macro_use] #[macro_use]
extern crate include_dir; extern crate include_dir;
#[macro_use] #[macro_use]
extern crate nom; extern crate nom;
#[cfg(test)]
#[macro_use] #[macro_use]
extern crate matches; extern crate matches;
extern crate futures;
#[macro_use] #[macro_use]
mod util; mod util;
@ -53,6 +46,7 @@ use backtrace::Backtrace;
use std::io::{self, StdinLock, StdoutLock}; use std::io::{self, StdinLock, StdoutLock};
use std::panic; use std::panic;
use termion;
use termion::raw::IntoRawMode; use termion::raw::IntoRawMode;
use termion::raw::RawTerminal; use termion::raw::RawTerminal;

View file

@ -10,7 +10,7 @@ use alga::general::{
use std::collections::{hash_map, BTreeMap, HashMap}; use std::collections::{hash_map, BTreeMap, HashMap};
use std::iter::FromIterator; use std::iter::FromIterator;
#[derive(Debug, Clone)] #[derive(Debug, Clone, Default)]
pub struct EntityMap<A> { pub struct EntityMap<A> {
by_position: BTreeMap<Position, Vec<EntityID>>, by_position: BTreeMap<Position, Vec<EntityID>>,
by_id: HashMap<EntityID, A>, by_id: HashMap<EntityID, A>,
@ -24,7 +24,7 @@ impl<A: PartialEq> PartialEq for EntityMap<A> {
} }
impl<A: Eq> Eq for EntityMap<A> {} impl<A: Eq> Eq for EntityMap<A> {}
const BY_POS_INVARIANT: &'static str = const BY_POS_INVARIANT: &str =
"Invariant: All references in EntityMap.by_position should point to existent references in by_id"; "Invariant: All references in EntityMap.by_position should point to existent references in by_id";
impl<A> EntityMap<A> { impl<A> EntityMap<A> {
@ -54,26 +54,26 @@ impl<A> EntityMap<A> {
/// Remove all entities at the given position /// Remove all entities at the given position
pub fn remove_all_at(&mut self, pos: Position) { pub fn remove_all_at(&mut self, pos: Position) {
self.by_position.remove(&pos).map(|eids| { if let Some(eids) = self.by_position.remove(&pos) {
for eid in eids { for eid in eids {
self.by_id.remove(&eid).expect(BY_POS_INVARIANT); self.by_id.remove(&eid).expect(BY_POS_INVARIANT);
} }
}); }
} }
pub fn get<'a>(&'a self, id: EntityID) -> Option<&'a A> { pub fn get(&self, id: EntityID) -> Option<&A> {
self.by_id.get(&id) self.by_id.get(&id)
} }
pub fn get_mut<'a>(&'a mut self, id: EntityID) -> Option<&'a mut A> { pub fn get_mut(&mut self, id: EntityID) -> Option<&mut A> {
self.by_id.get_mut(&id) self.by_id.get_mut(&id)
} }
pub fn entities<'a>(&'a self) -> impl Iterator<Item = &'a A> { pub fn entities(&self) -> impl Iterator<Item = &A> {
self.by_id.values() self.by_id.values()
} }
pub fn entities_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut A> { pub fn entities_mut(&mut self) -> impl Iterator<Item = &mut A> {
self.by_id.values_mut() self.by_id.values_mut()
} }
@ -81,8 +81,8 @@ impl<A> EntityMap<A> {
self.by_id.keys() self.by_id.keys()
} }
pub fn drain<'a>(&'a mut self) -> Drain<'a, A> { pub fn drain(&mut self) -> Drain<'_, A> {
let ids = self.ids().map(|e| *e).collect::<Vec<_>>(); let ids = self.ids().copied().collect::<Vec<_>>();
Drain { Drain {
map: self, map: self,
ids_iter: Box::new(ids.into_iter()), ids_iter: Box::new(ids.into_iter()),
@ -103,7 +103,7 @@ impl<A: Positioned + Identified<EntityID>> EntityMap<A> {
self.by_id.entry(entity_id).or_insert(entity); self.by_id.entry(entity_id).or_insert(entity);
self.by_position self.by_position
.entry(pos) .entry(pos)
.or_insert(Vec::new()) .or_insert_with(Vec::new)
.push(entity_id); .push(entity_id);
entity_id entity_id
} }
@ -113,12 +113,14 @@ impl<A: Positioned + Identified<EntityID>> EntityMap<A> {
self.by_id.remove(&id).map(|e| { self.by_id.remove(&id).map(|e| {
let mut empty = false; let mut empty = false;
let position = e.position(); let position = e.position();
self.by_position.get_mut(&position).map(|es| {
if let Some(es) = self.by_position.get_mut(&position) {
es.retain(|e| *e != id); es.retain(|e| *e != id);
if es.len() == 0 { if es.is_empty() {
empty = true; empty = true;
} }
}); }
if empty { if empty {
self.by_position.remove(&position); self.by_position.remove(&position);
} }
@ -172,7 +174,7 @@ impl<'a, A: Positioned + Identified<EntityID>> IntoIterator
type Item = (&'a EntityID, &'a A); type Item = (&'a EntityID, &'a A);
type IntoIter = std::collections::hash_map::Iter<'a, EntityID, A>; type IntoIter = std::collections::hash_map::Iter<'a, EntityID, A>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
(&self.by_id).into_iter() (&self.by_id).iter()
} }
} }
@ -246,20 +248,21 @@ impl<A: PositionedMut> EntityMap<A> {
old_pos = Some(entity.position()); old_pos = Some(entity.position());
entity.set_position(new_position); entity.set_position(new_position);
} }
old_pos.map(|p| {
self.by_position if let Some(p) = old_pos {
.get_mut(&p) if let Some(es) = self.by_position.get_mut(&p) {
.map(|es| es.retain(|e| *e != entity_id)); es.retain(|e| *e != entity_id);
}
self.by_position self.by_position
.entry(new_position) .entry(new_position)
.or_insert(Vec::new()) .or_insert_with(Vec::new)
.push(entity_id); .push(entity_id);
}); }
} }
} }
pub struct Drain<'a, A: 'a> { pub struct Drain<'a, A> {
map: &'a mut EntityMap<A>, map: &'a mut EntityMap<A>,
ids_iter: Box<dyn Iterator<Item = EntityID> + 'a>, ids_iter: Box<dyn Iterator<Item = EntityID> + 'a>,
} }
@ -313,9 +316,7 @@ mod tests {
fn gen_entity_map() -> BoxedStrategy<EntityMap<TestEntity>> { fn gen_entity_map() -> BoxedStrategy<EntityMap<TestEntity>> {
any::<Vec<TestEntity>>() any::<Vec<TestEntity>>()
.prop_map(|ents| { .prop_map(|ents| {
ents.iter() ents.iter().cloned().collect::<EntityMap<TestEntity>>()
.map(|e| e.clone())
.collect::<EntityMap<TestEntity>>()
}) })
.boxed() .boxed()
} }

View file

@ -427,6 +427,7 @@ impl<A> Neighbors<Vec<A>> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#![allow(clippy::unnecessary_operation)]
use super::*; use super::*;
use proptest::prelude::*; use proptest::prelude::*;

View file

@ -3,9 +3,11 @@ use std::pin::Pin;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
use std::task::{Context, Poll, Waker}; use std::task::{Context, Poll, Waker};
type Waiter<Env, T> = Box<dyn Fn(&mut Env, &T)>;
pub struct Promise<Env, T> { pub struct Promise<Env, T> {
inner: Arc<RwLock<Inner<T>>>, inner: Arc<RwLock<Inner<T>>>,
waiters: Arc<RwLock<Vec<Box<dyn Fn(&mut Env, &T)>>>>, waiters: Arc<RwLock<Vec<Waiter<Env, T>>>>,
} }
pub struct Complete<T> { pub struct Complete<T> {
@ -29,7 +31,7 @@ pub fn promise<Env, T>() -> (Complete<T>, Promise<Env, T>) {
inner: inner.clone(), inner: inner.clone(),
waiters: Arc::new(RwLock::new(Vec::new())), waiters: Arc::new(RwLock::new(Vec::new())),
}; };
let complete = Complete { inner: inner }; let complete = Complete { inner };
(complete, promise) (complete, promise)
} }
@ -127,7 +129,7 @@ impl<Env, P: Give<Env>> Give<Env> for &P {
impl<Env, T> Future for Promise<Env, T> { impl<Env, T> Future for Promise<Env, T> {
type Output = Arc<T>; type Output = Arc<T>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut inner = self.inner.write().unwrap(); let mut inner = self.inner.write().unwrap();
match inner.value { match inner.value {
Some(ref v) => Poll::Ready(v.clone()), Some(ref v) => Poll::Ready(v.clone()),

View file

@ -18,7 +18,7 @@ impl<'a> Path<'a> {
} }
impl<'a> Display for Path<'a> { impl<'a> Display for Path<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.head)?; write!(f, "{}", self.head)?;
for part in &self.tail { for part in &self.tail {
write!(f, ".{}", part)?; write!(f, ".{}", part)?;
@ -96,7 +96,7 @@ impl<'a> TemplateVisitor<'a> {
impl<'a> serde::de::Visitor<'a> for TemplateVisitor<'a> { impl<'a> serde::de::Visitor<'a> for TemplateVisitor<'a> {
type Value = Template<'a>; type Value = Template<'a>;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a valid template string") formatter.write_str("a valid template string")
} }
@ -126,7 +126,7 @@ impl<'a> Template<'a> {
input: &'a str, input: &'a str,
) -> Result<Template<'a>, Err<(&'a str, ErrorKind)>> { ) -> Result<Template<'a>, Err<(&'a str, ErrorKind)>> {
let (remaining, res) = template(input)?; let (remaining, res) = template(input)?;
if remaining.len() > 0 { if !remaining.is_empty() {
unreachable!(); unreachable!();
} }
Ok(res) Ok(res)
@ -157,7 +157,7 @@ pub enum TemplateError<'a> {
} }
impl<'a> Display for TemplateError<'a> { impl<'a> Display for TemplateError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use TemplateError::*; use TemplateError::*;
match self { match self {
MissingParam(path) => { MissingParam(path) => {
@ -179,7 +179,7 @@ impl<'a> TemplateParams<'a> {
match self { match self {
Direct(_) => None, Direct(_) => None,
Nested(m) => m.get(path.head).and_then(|next| { Nested(m) => m.get(path.head).and_then(|next| {
if path.tail.len() == 0 { if path.tail.is_empty() {
match next { match next {
Direct(s) => Some(*s), Direct(s) => Some(*s),
_ => None, _ => None,