From f48757c5e7d046f7ad87bbe3bc8f3cf2bab882c4 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 8 Dec 2018 18:25:05 -0800 Subject: [PATCH] clippy pt2 --- clippy.sh | 5 +- convert_osm/src/neighborhoods.rs | 2 +- editor/src/plugins/sim/show_score.rs | 2 +- editor/src/plugins/time_travel.rs | 12 ++- ezgui/src/lib.rs | 2 +- ezgui/src/text.rs | 2 +- ezgui/src/text_box.rs | 2 +- ezgui/src/tree_menu.rs | 12 +-- ezgui/src/wizard.rs | 4 +- geom/src/polyline.rs | 4 +- map_model/src/intersection.rs | 4 +- map_model/src/make/buildings.rs | 2 +- map_model/src/make/bus_stops.rs | 2 +- map_model/src/make/intersections.rs | 3 +- map_model/src/make/sidewalk_finder.rs | 2 +- map_model/src/make/turns.rs | 2 +- map_model/src/map.rs | 13 ++-- map_model/src/pathfind.rs | 11 +-- map_model/src/road.rs | 10 +-- map_model/src/traversable.rs | 42 +++++------ playground_gui/src/debug_intersection.rs | 1 + playground_gui/src/debug_polygon_drawing.rs | 1 + playground_gui/src/debug_polyline.rs | 1 + playground_gui/src/trim_polyline.rs | 1 + sim/src/driving.rs | 7 +- sim/src/helpers.rs | 5 +- sim/src/instrument.rs | 8 +- sim/src/intersections.rs | 83 +++++++++------------ sim/src/kinematics.rs | 2 +- sim/src/make/a_b_test.rs | 2 +- sim/src/make/scenario.rs | 64 ++++++++-------- sim/src/parking.rs | 8 +- sim/src/physics.rs | 22 +++--- sim/src/sim.rs | 2 +- sim/src/spawn.rs | 6 +- sim/src/trips.rs | 21 +++--- sim/src/walking.rs | 11 +-- synthetic/src/main.rs | 4 +- tests/src/geom.rs | 1 + 39 files changed, 179 insertions(+), 209 deletions(-) diff --git a/clippy.sh b/clippy.sh index a54a063bcc..52c190a16c 100755 --- a/clippy.sh +++ b/clippy.sh @@ -5,6 +5,7 @@ touch `find * | grep '\.rs' | grep -v target | xargs` # TODO Remove all of these exceptions cargo clippy -- \ + -A clippy::cyclomatic_complexity \ -A clippy::expect_fun_call \ -A clippy::if_same_then_else \ -A clippy::needless_pass_by_value \ @@ -13,4 +14,6 @@ cargo clippy -- \ -A clippy::new_without_default_derive \ -A clippy::ptr_arg \ -A clippy::suspicious_arithmetic_impl \ - -A clippy::type_complexity + -A clippy::too_many_arguments \ + -A clippy::type_complexity \ + -A clippy::wrong_self_convention diff --git a/convert_osm/src/neighborhoods.rs b/convert_osm/src/neighborhoods.rs index 66bc336b9e..51e3d1dda4 100644 --- a/convert_osm/src/neighborhoods.rs +++ b/convert_osm/src/neighborhoods.rs @@ -53,7 +53,7 @@ fn convert_polygon(input: PolygonType, name: String, map_name: String, gps_bound } } NeighborhoodBuilder { - map_name: map_name, + map_name, name, points, } diff --git a/editor/src/plugins/sim/show_score.rs b/editor/src/plugins/sim/show_score.rs index e34dc6f5aa..77d26ec373 100644 --- a/editor/src/plugins/sim/show_score.rs +++ b/editor/src/plugins/sim/show_score.rs @@ -60,7 +60,7 @@ fn panel(ctx: &mut PluginCtx) -> State { // TODO More coloring txt.add_line(ctx.primary.sim.get_name().to_string()); summarize(&mut txt, ctx.primary.sim.get_score()); - txt.add_line("".to_string()); + txt.add_line(String::new()); txt.add_line(s.sim.get_name().to_string()); summarize(&mut txt, s.sim.get_score()); } else { diff --git a/editor/src/plugins/time_travel.rs b/editor/src/plugins/time_travel.rs index dee3ba08f3..d517552ce3 100644 --- a/editor/src/plugins/time_travel.rs +++ b/editor/src/plugins/time_travel.rs @@ -81,13 +81,11 @@ impl Plugin for TimeTravel { } else if ctx.input.key_pressed(Key::Return, "exit time traveler") { self.current_tick = None; } - } else { - if ctx - .input - .unimportant_key_pressed(Key::T, SIM, "start time traveling") - { - self.current_tick = Some(ctx.primary.sim.time); - } + } else if ctx + .input + .unimportant_key_pressed(Key::T, SIM, "start time traveling") + { + self.current_tick = Some(ctx.primary.sim.time); } if let Some(tick) = self.current_tick { diff --git a/ezgui/src/lib.rs b/ezgui/src/lib.rs index 6e748ec922..a4dc1f34dd 100644 --- a/ezgui/src/lib.rs +++ b/ezgui/src/lib.rs @@ -123,7 +123,7 @@ impl<'a> GfxCtx<'a> { pub fn draw_polygon(&mut self, color: Color, poly: &geom::Polygon) { for tri in &poly.triangles { graphics::Polygon::new(color.0).draw( - &vec![ + &[ [tri.pt1.x(), tri.pt1.y()], [tri.pt2.x(), tri.pt2.y()], [tri.pt3.x(), tri.pt3.y()], diff --git a/ezgui/src/text.rs b/ezgui/src/text.rs index d5677318f6..74a59fb972 100644 --- a/ezgui/src/text.rs +++ b/ezgui/src/text.rs @@ -87,7 +87,7 @@ impl Text { .iter() .max_by_key(|l| l.iter().fold(0, |so_far, span| so_far + span.text.len())) .unwrap(); - let mut concat = String::from(""); + let mut concat = String::new(); for span in longest_line { concat.push_str(&span.text); } diff --git a/ezgui/src/text_box.rs b/ezgui/src/text_box.rs index 22ed430d86..f95d14fbf8 100644 --- a/ezgui/src/text_box.rs +++ b/ezgui/src/text_box.rs @@ -17,7 +17,7 @@ impl TextBox { pub fn new(prompt: &str, prefilled: Option) -> TextBox { TextBox { prompt: prompt.to_string(), - line: prefilled.unwrap_or(String::from("")), + line: prefilled.unwrap_or_else(String::new), cursor_x: 0, shift_pressed: false, } diff --git a/ezgui/src/tree_menu.rs b/ezgui/src/tree_menu.rs index 831b654247..c72378a12b 100644 --- a/ezgui/src/tree_menu.rs +++ b/ezgui/src/tree_menu.rs @@ -21,7 +21,7 @@ impl TreeMenu { return; } - let parts: Vec<&str> = path.split("/").collect(); + let parts: Vec<&str> = path.split('/').collect(); populate_tree(VecDeque::from(parts), &mut self.root, hotkey, action); } } @@ -43,10 +43,10 @@ fn print(depth: usize, tree: &BTreeMap, f: &mut fmt::Formatter) -> for (name, item) in tree { match item { Item::Action(key) => { - writeln!(f, "{}- {} ({})", pad, name, describe_maybe_key(key))?; + writeln!(f, "{}- {} ({})", pad, name, describe_maybe_key(*key))?; } Item::Tree(key, subtree) => { - writeln!(f, "{}- {} ({})", pad, name, describe_maybe_key(key))?; + writeln!(f, "{}- {} ({})", pad, name, describe_maybe_key(*key))?; print(depth + 1, subtree, f)?; } } @@ -54,10 +54,10 @@ fn print(depth: usize, tree: &BTreeMap, f: &mut fmt::Formatter) -> Ok(()) } -fn describe_maybe_key(key: &Option) -> String { +fn describe_maybe_key(key: Option) -> String { match key { - Some(k) => describe_key(*k), - None => "".to_string(), + Some(k) => describe_key(k), + None => String::new(), } } diff --git a/ezgui/src/wizard.rs b/ezgui/src/wizard.rs index 9178d7dc17..a0d6d41ac7 100644 --- a/ezgui/src/wizard.rs +++ b/ezgui/src/wizard.rs @@ -131,11 +131,11 @@ impl<'a> WrappedWizard<'a> { } pub fn input_string(&mut self, query: &str) -> Option { - self.input_something(query, None, Box::new(|line| Some(line))) + self.input_something(query, None, Box::new(Some)) } pub fn input_string_prefilled(&mut self, query: &str, prefilled: String) -> Option { - self.input_something(query, Some(prefilled), Box::new(|line| Some(line))) + self.input_something(query, Some(prefilled), Box::new(Some)) } pub fn input_usize(&mut self, query: &str) -> Option { diff --git a/geom/src/polyline.rs b/geom/src/polyline.rs index 776cd2e1ce..17ed7c8575 100644 --- a/geom/src/polyline.rs +++ b/geom/src/polyline.rs @@ -165,7 +165,7 @@ impl PolyLine { let l2 = Line::new(pt2_raw, pt3_raw).shift(width); // When the lines are perfectly parallel, it means pt2_shift_1st == pt2_shift_2nd and the // original geometry is redundant. - let pt2_shift = line_intersection(&l1, &l2).unwrap_or(l1.pt2()); + let pt2_shift = line_intersection(&l1, &l2).unwrap_or_else(|| l1.pt2()); if pt3_idx == 2 { result.push(l1.pt1()); @@ -226,7 +226,7 @@ impl PolyLine { let l2 = Line::new(pt2_raw, pt3_raw).shift(width); // When the lines are perfectly parallel, it means pt2_shift_1st == pt2_shift_2nd and the // original geometry is redundant. - let pt2_shift = line_intersection(&l1, &l2).unwrap_or(l1.pt2()); + let pt2_shift = line_intersection(&l1, &l2).unwrap_or_else(|| l1.pt2()); if pt3_idx == 2 { result.push(l1.pt1()); diff --git a/map_model/src/intersection.rs b/map_model/src/intersection.rs index 44a78fdd68..81ca3d23bf 100644 --- a/map_model/src/intersection.rs +++ b/map_model/src/intersection.rs @@ -58,7 +58,7 @@ impl Intersection { self.incoming_lanes .iter() .filter(|l| map.get_l(**l).lane_type == lt) - .map(|l| *l) + .cloned() .collect() } @@ -66,7 +66,7 @@ impl Intersection { self.outgoing_lanes .iter() .filter(|l| map.get_l(**l).lane_type == lt) - .map(|l| *l) + .cloned() .collect() } diff --git a/map_model/src/make/buildings.rs b/map_model/src/make/buildings.rs index 4dd775b8c1..ab656901c2 100644 --- a/map_model/src/make/buildings.rs +++ b/map_model/src/make/buildings.rs @@ -52,7 +52,7 @@ pub fn make_all_buildings( osm_way_id: input[idx].osm_way_id, front_path: FrontPath { bldg: id, - sidewalk: sidewalk_pos.clone(), + sidewalk: *sidewalk_pos, line, }, }); diff --git a/map_model/src/make/bus_stops.rs b/map_model/src/make/bus_stops.rs index aedff2bec6..29d2947837 100644 --- a/map_model/src/make/bus_stops.rs +++ b/map_model/src/make/bus_stops.rs @@ -76,7 +76,7 @@ pub fn make_bus_stops( let stops: Vec = stop_points .iter() .filter_map(|pt| point_to_stop_id.get(pt)) - .map(|stop| *stop) + .cloned() .collect(); if stops.len() < 2 { warn!( diff --git a/map_model/src/make/intersections.rs b/map_model/src/make/intersections.rs index b4aac88bbc..5981f7aa67 100644 --- a/map_model/src/make/intersections.rs +++ b/map_model/src/make/intersections.rs @@ -143,7 +143,6 @@ pub fn intersection_polygon(i: &Intersection, roads: &Vec) -> Vec { } // Close off the polygon - let first_pt = endpoints[0].clone(); - endpoints.push(first_pt); + endpoints.push(endpoints[0]); endpoints } diff --git a/map_model/src/make/sidewalk_finder.rs b/map_model/src/make/sidewalk_finder.rs index 1703b44373..0d83b1b45b 100644 --- a/map_model/src/make/sidewalk_finder.rs +++ b/map_model/src/make/sidewalk_finder.rs @@ -34,7 +34,7 @@ pub fn find_sidewalk_points( timer.next(); if let Some((sidewalk, sidewalk_pt)) = closest.closest_pt(query_pt.into(), max_dist_away) { if let Some(dist_along) = lanes[sidewalk.0].dist_along_of_point(sidewalk_pt) { - results.insert(query_pt.into(), Position::new(sidewalk, dist_along)); + results.insert(query_pt, Position::new(sidewalk, dist_along)); } else { panic!("{} isn't on {} according to dist_along_of_point, even though closest_point thinks it is.\n{}", sidewalk_pt, sidewalk, lanes[sidewalk.0].lane_center_pts); } diff --git a/map_model/src/make/turns.rs b/map_model/src/make/turns.rs index 484773f819..605e0b872e 100644 --- a/map_model/src/make/turns.rs +++ b/map_model/src/make/turns.rs @@ -318,7 +318,7 @@ fn make_vehicle_turn(map: &Map, i: IntersectionID, l1: LaneID, l2: LaneID) -> Tu let pieces = 5; PolyLine::new( (0..=pieces) - .map(|i| from_pt(curve.interp(1.0 / (pieces as f64) * (i as f64)).unwrap())) + .map(|i| from_pt(curve.interp(1.0 / f64::from(i * pieces)).unwrap())) .collect(), ) }; diff --git a/map_model/src/map.rs b/map_model/src/map.rs index 5a7e888818..57a568f861 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -172,7 +172,7 @@ impl Map { // TODO probably different behavior for oneways // TODO need to factor in yellow center lines (but what's the right thing to even do? // Reverse points for British-style driving on the left - let width = LANE_THICKNESS * ((lane.offset as f64) + 0.5); + let width = LANE_THICKNESS * (0.5 + f64::from(lane.offset)); let (lane_center_pts, probably_broken) = match unshifted_pts.shift(width) { Some(pts) => (pts, false), // TODO wasteful to calculate again, but eh @@ -465,7 +465,7 @@ impl Map { } pub fn lookup_turn_by_idx(&self, idx: usize) -> Option { - self.turn_lookup.get(idx).map(|id| *id) + self.turn_lookup.get(idx).cloned() } // All these helpers should take IDs and return objects. @@ -668,8 +668,7 @@ impl Map { } else if let Some(ts) = self.traffic_signals.get(&t.parent) { ts.cycles .iter() - .find(|c| c.get_priority(t) != TurnPriority::Banned) - .is_some() + .any(|c| c.get_priority(t) != TurnPriority::Banned) } else { // Border nodes have no turns... panic!("{}'s intersection isn't a stop sign or traffic signal", t); @@ -685,12 +684,10 @@ fn is_border(intersection: &Intersection, map: &Map) -> bool { let has_driving_in = intersection .incoming_lanes .iter() - .find(|l| map.get_l(**l).is_driving()) - .is_some(); + .any(|l| map.get_l(*l).is_driving()); let has_driving_out = intersection .outgoing_lanes .iter() - .find(|l| map.get_l(**l).is_driving()) - .is_some(); + .any(|l| map.get_l(*l).is_driving()); has_driving_in != has_driving_out } diff --git a/map_model/src/pathfind.rs b/map_model/src/pathfind.rs index 7dea8257c7..dafcea459e 100644 --- a/map_model/src/pathfind.rs +++ b/map_model/src/pathfind.rs @@ -230,7 +230,7 @@ impl Path { } } - return Some(pts_so_far.unwrap()); + Some(pts_so_far.unwrap()) } pub fn get_steps(&self) -> &VecDeque { @@ -285,7 +285,7 @@ impl Pathfinder { steps.last().unwrap().as_traversable(), Traversable::Lane(req.end.lane()) ); - return Some(Path::new(map, steps, req.end.dist_along())); + Some(Path::new(map, steps, req.end.dist_along())) } // Attempt the pathfinding and see if riding a bus is a step. @@ -435,12 +435,13 @@ impl Pathfinder { // Expand for next in self.expand(map, current).into_iter() { - if !backrefs.contains_key(&next) { - backrefs.insert(next, current); + backrefs.entry(next).or_insert_with(|| { let cost = next.cost(map); let heuristic = next.heuristic(self.goal_pt, map); queue.push((dist_to_pri_queue(cost + heuristic) + cost_sofar, next)); - } + + current + }); } } diff --git a/map_model/src/road.rs b/map_model/src/road.rs index 6b63b897f5..ffe06d2ad4 100644 --- a/map_model/src/road.rs +++ b/map_model/src/road.rs @@ -52,17 +52,11 @@ impl Road { } pub fn is_forwards(&self, lane: LaneID) -> bool { - self.children_forwards - .iter() - .find(|(id, _)| *id == lane) - .is_some() + self.children_forwards.iter().any(|(id, _)| *id == lane) } pub fn is_backwards(&self, lane: LaneID) -> bool { - self.children_backwards - .iter() - .find(|(id, _)| *id == lane) - .is_some() + self.children_backwards.iter().any(|(id, _)| *id == lane) } // lane must belong to this road. Offset 0 is the centermost lane on each side of a road, then diff --git a/map_model/src/traversable.rs b/map_model/src/traversable.rs index 81c952d3a7..1539fecab1 100644 --- a/map_model/src/traversable.rs +++ b/map_model/src/traversable.rs @@ -55,52 +55,52 @@ pub enum Traversable { impl Traversable { pub fn as_lane(&self) -> LaneID { - match self { - &Traversable::Lane(id) => id, - &Traversable::Turn(_) => panic!("not a lane"), + match *self { + Traversable::Lane(id) => id, + Traversable::Turn(_) => panic!("not a lane"), } } pub fn as_turn(&self) -> TurnID { - match self { - &Traversable::Turn(id) => id, - &Traversable::Lane(_) => panic!("not a turn"), + match *self { + Traversable::Turn(id) => id, + Traversable::Lane(_) => panic!("not a turn"), } } pub fn maybe_turn(&self) -> Option { - match self { - &Traversable::Turn(id) => Some(id), - &Traversable::Lane(_) => None, + match *self { + Traversable::Turn(id) => Some(id), + Traversable::Lane(_) => None, } } pub fn maybe_lane(&self) -> Option { - match self { - &Traversable::Turn(_) => None, - &Traversable::Lane(id) => Some(id), + match *self { + Traversable::Turn(_) => None, + Traversable::Lane(id) => Some(id), } } // TODO Just expose the PolyLine instead of all these layers of helpers pub fn length(&self, map: &Map) -> si::Meter { - match self { - &Traversable::Lane(id) => map.get_l(id).length(), - &Traversable::Turn(id) => map.get_t(id).length(), + match *self { + Traversable::Lane(id) => map.get_l(id).length(), + Traversable::Turn(id) => map.get_t(id).length(), } } pub fn dist_along(&self, dist: si::Meter, map: &Map) -> (Pt2D, Angle) { - match self { - &Traversable::Lane(id) => map.get_l(id).dist_along(dist), - &Traversable::Turn(id) => map.get_t(id).dist_along(dist), + match *self { + Traversable::Lane(id) => map.get_l(id).dist_along(dist), + Traversable::Turn(id) => map.get_t(id).dist_along(dist), } } pub fn speed_limit(&self, map: &Map) -> si::MeterPerSecond { - match self { - &Traversable::Lane(id) => map.get_parent(id).get_speed_limit(), - &Traversable::Turn(id) => map.get_parent(id.dst).get_speed_limit(), + match *self { + Traversable::Lane(id) => map.get_parent(id).get_speed_limit(), + Traversable::Turn(id) => map.get_parent(id.dst).get_speed_limit(), } } } diff --git a/playground_gui/src/debug_intersection.rs b/playground_gui/src/debug_intersection.rs index a15d3ad653..b02058bde4 100644 --- a/playground_gui/src/debug_intersection.rs +++ b/playground_gui/src/debug_intersection.rs @@ -5,6 +5,7 @@ use geom::{PolyLine, Pt2D}; // Copied from map_model; no need to have to rebuild that crate const LANE_THICKNESS: f64 = 2.5; +#[allow(clippy::unreadable_literal)] pub fn run(g: &mut GfxCtx) { let thin = 0.25; let shift1_width = LANE_THICKNESS * 0.5; diff --git a/playground_gui/src/debug_polygon_drawing.rs b/playground_gui/src/debug_polygon_drawing.rs index 8ef3605f32..27578bedb8 100644 --- a/playground_gui/src/debug_polygon_drawing.rs +++ b/playground_gui/src/debug_polygon_drawing.rs @@ -2,6 +2,7 @@ use crate::common::BLUE; use ezgui::GfxCtx; use geom::{Polygon, Pt2D}; +#[allow(clippy::unreadable_literal)] pub fn run(g: &mut GfxCtx, labels: &mut Vec<(Pt2D, String)>) { let pts = vec![ Pt2D::new(1158.5480421283125, 759.4168710122531), // 0 diff --git a/playground_gui/src/debug_polyline.rs b/playground_gui/src/debug_polyline.rs index fe3496da30..3f1f1dcf44 100644 --- a/playground_gui/src/debug_polyline.rs +++ b/playground_gui/src/debug_polyline.rs @@ -2,6 +2,7 @@ use crate::common::{draw_polyline, BLACK, RED}; use ezgui::GfxCtx; use geom::{PolyLine, Pt2D}; +#[allow(clippy::unreadable_literal)] pub fn run(g: &mut GfxCtx, labels: &mut Vec<(Pt2D, String)>) { let thin = 1.0; let width = 50.0; diff --git a/playground_gui/src/trim_polyline.rs b/playground_gui/src/trim_polyline.rs index d9a1484b54..3cda16afc8 100644 --- a/playground_gui/src/trim_polyline.rs +++ b/playground_gui/src/trim_polyline.rs @@ -2,6 +2,7 @@ use crate::common::{draw_polyline, BLUE, GREEN, RED}; use ezgui::GfxCtx; use geom::{Circle, PolyLine, Pt2D}; +#[allow(clippy::unreadable_literal)] pub fn run(g: &mut GfxCtx) { let mut vertical_pl = PolyLine::new(vec![ Pt2D::new(1333.9512635794777, 413.3946082988369), diff --git a/sim/src/driving.rs b/sim/src/driving.rs index 18a49d70ed..df3c13bf40 100644 --- a/sim/src/driving.rs +++ b/sim/src/driving.rs @@ -185,7 +185,8 @@ impl Car { parking_sim, transit_sim, ); - let dist_to_maybe_stop_at = maybe_stop_early.unwrap_or(current_on.length(map)); + let dist_to_maybe_stop_at = + maybe_stop_early.unwrap_or_else(|| current_on.length(map)); let dist_from_stop = dist_to_maybe_stop_at - current_dist_along; // If our lookahead doesn't even hit the intersection / early stopping point, then @@ -614,7 +615,7 @@ impl DrivingSimState { } Action::StartParkingBike => { { - let c = self.cars.get(&id).unwrap(); + let c = &self.cars[&id]; done_biking.push((*id, Position::new(c.on.as_lane(), c.dist_along))); } self.cars.remove(&id); @@ -822,7 +823,7 @@ impl DrivingSimState { if let Some(queue) = self.queues.get(&on) { return queue.get_draw_cars(self, map, time); } - return Vec::new(); + Vec::new() } pub fn get_all_draw_cars(&self, time: Tick, map: &Map) -> Vec { diff --git a/sim/src/helpers.rs b/sim/src/helpers.rs index b17347bad5..c6f98bfb0e 100644 --- a/sim/src/helpers.rs +++ b/sim/src/helpers.rs @@ -175,10 +175,7 @@ impl Sim { self.spawner.start_trip_using_parked_car( Tick::zero(), map, - self.parking_state - .lookup_car(car) - .map(|p| p.clone()) - .unwrap(), + self.parking_state.lookup_car(car).cloned().unwrap(), &self.parking_state, from_bldg, DrivingGoal::ParkNear(to_bldg), diff --git a/sim/src/instrument.rs b/sim/src/instrument.rs index e75ebe3df8..65d927bf00 100644 --- a/sim/src/instrument.rs +++ b/sim/src/instrument.rs @@ -16,7 +16,7 @@ pub fn capture_backtrace(event: &str) { // TODO compiler flag so capture_backtrace is usually a no-op. actually, looks like this // doesn't work in --release mode, so use that. let symbol_name = f.symbols()[0].name(); - if !symbol_name.is_some() { + if symbol_name.is_none() { return; } @@ -30,10 +30,8 @@ pub fn capture_backtrace(event: &str) { if name == "sim::sim::Sim::inner_step" { break; } - } else { - if name.ends_with("::capture_backtrace") { - found_this_fxn = true; - } + } else if name.ends_with("::capture_backtrace") { + found_this_fxn = true; } } diff --git a/sim/src/intersections.rs b/sim/src/intersections.rs index 7cb54a4eba..011ab6fd7d 100644 --- a/sim/src/intersections.rs +++ b/sim/src/intersections.rs @@ -51,13 +51,11 @@ impl IntersectionSimState { let mut intersections: Vec = Vec::new(); for i in map.all_intersections() { intersections.push(match i.intersection_type { - IntersectionType::StopSign => { - IntersectionPolicy::StopSignPolicy(StopSign::new(i.id)) - } + IntersectionType::StopSign => IntersectionPolicy::StopSign(StopSign::new(i.id)), IntersectionType::TrafficSignal => { - IntersectionPolicy::TrafficSignalPolicy(TrafficSignal::new(i.id)) + IntersectionPolicy::TrafficSignal(TrafficSignal::new(i.id)) } - IntersectionType::Border => IntersectionPolicy::BorderPolicy, + IntersectionType::Border => IntersectionPolicy::Border, }); } IntersectionSimState { @@ -78,31 +76,29 @@ impl IntersectionSimState { // agent is the leader vehicle and at the end of the lane). The request may have been // previously granted, but the agent might not have been able to start the turn. pub fn submit_request(&mut self, req: Request) { - let i = self.intersections.get_mut(req.turn.parent.0).unwrap(); + let i = &mut self.intersections[req.turn.parent.0]; if i.is_accepted(&req) { return; } match i { - IntersectionPolicy::StopSignPolicy(ref mut p) => { + IntersectionPolicy::StopSign(ref mut p) => { if !p.started_waiting_at.contains_key(&req) { p.approaching_agents.insert(req); } } - IntersectionPolicy::TrafficSignalPolicy(ref mut p) => { + IntersectionPolicy::TrafficSignal(ref mut p) => { p.requests.insert(req); } - IntersectionPolicy::BorderPolicy => {} + IntersectionPolicy::Border => {} } } pub fn step(&mut self, events: &mut Vec, time: Tick, map: &Map, view: &WorldView) { for i in self.intersections.iter_mut() { match i { - IntersectionPolicy::StopSignPolicy(ref mut p) => p.step(events, time, map, view), - IntersectionPolicy::TrafficSignalPolicy(ref mut p) => { - p.step(events, time, map, view) - } - IntersectionPolicy::BorderPolicy => {} + IntersectionPolicy::StopSign(ref mut p) => p.step(events, time, map, view), + IntersectionPolicy::TrafficSignal(ref mut p) => p.step(events, time, map, view), + IntersectionPolicy::Border => {} } } } @@ -116,16 +112,16 @@ impl IntersectionSimState { } Ok(()) } else { - return Err(Error::new(format!( + Err(Error::new(format!( "{:?} entered, but wasn't accepted by the intersection yet", req - ))); + ))) } } pub fn on_exit(&mut self, req: Request) { let id = req.turn.parent; - let i = self.intersections.get_mut(id.0).unwrap(); + let i = &mut self.intersections[id.0]; assert!(i.is_accepted(&req)); i.on_exit(&req); if self.debug == Some(id) { @@ -135,28 +131,28 @@ impl IntersectionSimState { pub fn debug(&mut self, id: IntersectionID, map: &Map) { if let Some(old) = self.debug { - match self.intersections.get_mut(old.0).unwrap() { - IntersectionPolicy::StopSignPolicy(ref mut p) => { + match self.intersections[old.0] { + IntersectionPolicy::StopSign(ref mut p) => { p.debug = false; } - IntersectionPolicy::TrafficSignalPolicy(ref mut p) => { + IntersectionPolicy::TrafficSignal(ref mut p) => { p.debug = false; } - IntersectionPolicy::BorderPolicy => {} + IntersectionPolicy::Border => {} }; } println!("{}", abstutil::to_json(&self.intersections[id.0])); - match self.intersections.get_mut(id.0).unwrap() { - IntersectionPolicy::StopSignPolicy(ref mut p) => { + match self.intersections[id.0] { + IntersectionPolicy::StopSign(ref mut p) => { p.debug = true; println!("{}", abstutil::to_json(map.get_stop_sign(id))); } - IntersectionPolicy::TrafficSignalPolicy(ref mut p) => { + IntersectionPolicy::TrafficSignal(ref mut p) => { p.debug = true; println!("{}", abstutil::to_json(map.get_traffic_signal(id))); } - IntersectionPolicy::BorderPolicy => { + IntersectionPolicy::Border => { println!("{} is a border", id); } }; @@ -166,25 +162,25 @@ impl IntersectionSimState { // Use an enum instead of traits so that serialization works. I couldn't figure out erased_serde. #[derive(Serialize, Deserialize, PartialEq)] enum IntersectionPolicy { - StopSignPolicy(StopSign), - TrafficSignalPolicy(TrafficSignal), - BorderPolicy, + StopSign(StopSign), + TrafficSignal(TrafficSignal), + Border, } impl IntersectionPolicy { fn is_accepted(&self, req: &Request) -> bool { match self { - IntersectionPolicy::StopSignPolicy(ref p) => p.accepted.contains(req), - IntersectionPolicy::TrafficSignalPolicy(ref p) => p.accepted.contains(req), - IntersectionPolicy::BorderPolicy => true, + IntersectionPolicy::StopSign(ref p) => p.accepted.contains(req), + IntersectionPolicy::TrafficSignal(ref p) => p.accepted.contains(req), + IntersectionPolicy::Border => true, } } fn on_exit(&mut self, req: &Request) { match self { - IntersectionPolicy::StopSignPolicy(ref mut p) => p.accepted.remove(&req), - IntersectionPolicy::TrafficSignalPolicy(ref mut p) => p.accepted.remove(&req), - IntersectionPolicy::BorderPolicy => { + IntersectionPolicy::StopSign(ref mut p) => p.accepted.remove(&req), + IntersectionPolicy::TrafficSignal(ref mut p) => p.accepted.remove(&req), + IntersectionPolicy::Border => { panic!("{:?} called on_exit for a border node; how?!", req); } }; @@ -225,8 +221,7 @@ impl StopSign { let base_t = map.get_t(turn); self.accepted .iter() - .find(|req| base_t.conflicts_with(map.get_t(req.turn))) - .is_some() + .any(|req| base_t.conflicts_with(map.get_t(req.turn))) } fn conflicts_with_waiting_with_higher_priority( @@ -237,13 +232,9 @@ impl StopSign { ) -> bool { let base_t = map.get_t(turn); let base_priority = ss.get_priority(turn); - self.started_waiting_at - .keys() - .find(|req| { - base_t.conflicts_with(map.get_t(req.turn)) - && ss.get_priority(req.turn) > base_priority - }) - .is_some() + self.started_waiting_at.keys().any(|req| { + base_t.conflicts_with(map.get_t(req.turn)) && ss.get_priority(req.turn) > base_priority + }) } fn step(&mut self, events: &mut Vec, time: Tick, map: &Map, view: &WorldView) { @@ -379,8 +370,7 @@ impl TrafficSignal { let base_t = map.get_t(req.turn); if priority_requests .iter() - .find(|t| base_t.conflicts_with(map.get_t(**t))) - .is_some() + .any(|t| base_t.conflicts_with(map.get_t(*t))) { keep_requests.insert(req.clone()); continue; @@ -407,7 +397,6 @@ impl TrafficSignal { let base_t = map.get_t(turn); self.accepted .iter() - .find(|req| base_t.conflicts_with(map.get_t(req.turn))) - .is_some() + .any(|req| base_t.conflicts_with(map.get_t(req.turn))) } } diff --git a/sim/src/kinematics.rs b/sim/src/kinematics.rs index 5bd5d1c9d1..20e199ccfd 100644 --- a/sim/src/kinematics.rs +++ b/sim/src/kinematics.rs @@ -8,7 +8,7 @@ use serde_derive::{Deserialize, Serialize}; use std; pub const EPSILON_SPEED: Speed = si::MeterPerSecond { - value_unsafe: 0.00000001, + value_unsafe: 0.000_000_01, _marker: std::marker::PhantomData, }; diff --git a/sim/src/make/a_b_test.rs b/sim/src/make/a_b_test.rs index c7e3b1be18..2f723f77f5 100644 --- a/sim/src/make/a_b_test.rs +++ b/sim/src/make/a_b_test.rs @@ -15,7 +15,7 @@ pub struct ABTest { impl ABTest { pub fn describe(&self) -> Vec { abstutil::to_json(self) - .split("\n") + .split('\n') .map(|s| s.to_string()) .collect() } diff --git a/sim/src/make/scenario.rs b/sim/src/make/scenario.rs index 658bf42d7a..e2ce7d54a4 100644 --- a/sim/src/make/scenario.rs +++ b/sim/src/make/scenario.rs @@ -56,7 +56,7 @@ pub struct SeedParkedCars { impl Scenario { pub fn describe(&self) -> Vec { abstutil::to_json(self) - .split("\n") + .split('\n') .map(|s| s.to_string()) .collect() } @@ -153,42 +153,40 @@ impl Scenario { &mut sim.rng, ); } - } else { - if let Some(goal) = - s.goal - .pick_walking_goal(map, &bldgs_per_neighborhood, &mut sim.rng) - { - let start_spot = SidewalkSpot::building(from_bldg, map); + } else if let Some(goal) = + s.goal + .pick_walking_goal(map, &bldgs_per_neighborhood, &mut sim.rng) + { + let start_spot = SidewalkSpot::building(from_bldg, map); - if sim.rng.gen_bool(s.percent_use_transit) { - // TODO This throws away some work. It also sequentially does expensive - // work right here. - if let Some((stop1, stop2, route)) = Pathfinder::should_use_transit( + if sim.rng.gen_bool(s.percent_use_transit) { + // TODO This throws away some work. It also sequentially does expensive + // work right here. + if let Some((stop1, stop2, route)) = Pathfinder::should_use_transit( + map, + start_spot.sidewalk_pos, + goal.sidewalk_pos, + ) { + sim.spawner.start_trip_using_bus( + spawn_time, map, - start_spot.sidewalk_pos, - goal.sidewalk_pos, - ) { - sim.spawner.start_trip_using_bus( - spawn_time, - map, - start_spot, - goal, - route, - stop1, - stop2, - &mut sim.trips_state, - ); - continue; - } + start_spot, + goal, + route, + stop1, + stop2, + &mut sim.trips_state, + ); + continue; } - - sim.spawner.start_trip_just_walking( - spawn_time, - start_spot, - goal, - &mut sim.trips_state, - ); } + + sim.spawner.start_trip_just_walking( + spawn_time, + start_spot, + goal, + &mut sim.trips_state, + ); } } } diff --git a/sim/src/parking.rs b/sim/src/parking.rs index 984b1393d1..7febf16ce5 100644 --- a/sim/src/parking.rs +++ b/sim/src/parking.rs @@ -29,7 +29,7 @@ impl ParkingSimState { pub fn edit_remove_lane(&mut self, id: LaneID) { assert!(self.lanes[id.0].is_empty()); self.lanes[id.0] = ParkingLane { - id: id, + id, spots: Vec::new(), occupants: Vec::new(), }; @@ -90,8 +90,8 @@ impl ParkingSimState { id: p.car, vehicle_length: p.vehicle.length, waiting_for_turn: None, - front: front, - angle: angle, + front, + angle, stopping_trace: None, state: CarState::Parked, vehicle_type: VehicleType::Car, @@ -224,7 +224,7 @@ impl ParkingLane { } fn is_empty(&self) -> bool { - !self.occupants.iter().find(|x| x.is_some()).is_some() + !self.occupants.iter().any(|x| x.is_some()) } } diff --git a/sim/src/physics.rs b/sim/src/physics.rs index 2b46065718..c4828eb1a6 100644 --- a/sim/src/physics.rs +++ b/sim/src/physics.rs @@ -20,7 +20,7 @@ pub type Acceleration = si::MeterPerSecond2; pub struct Tick(u32); impl Tick { - pub fn as_usize(&self) -> usize { + pub fn as_usize(self) -> usize { self.0 as usize } @@ -42,14 +42,14 @@ impl Tick { // TODO Why have these two forms? Consolidate pub fn parse(string: &str) -> Option { - let parts: Vec<&str> = string.split(":").collect(); + let parts: Vec<&str> = string.split(':').collect(); if parts.is_empty() { return None; } let mut ticks: u32 = 0; - if parts.last().unwrap().contains(".") { - let last_parts: Vec<&str> = parts.last().unwrap().split(".").collect(); + if parts.last().unwrap().contains('.') { + let last_parts: Vec<&str> = parts.last().unwrap().split('.').collect(); if last_parts.len() != 2 { return None; } @@ -87,8 +87,8 @@ impl Tick { Some(Tick(hours + minutes + seconds + ms)) } - pub fn as_time(&self) -> Time { - (self.0 as f64) * TIMESTEP + pub fn as_time(self) -> Time { + f64::from(self.0) * TIMESTEP } pub fn next(self) -> Tick { @@ -99,11 +99,11 @@ impl Tick { Tick(self.0 - 1) } - pub fn is_multiple_of(&self, other: Tick) -> bool { + pub fn is_multiple_of(self, other: Tick) -> bool { self.0 % other.0 == 0 } - fn get_parts(&self) -> (u32, u32, u32, u32) { + fn get_parts(self) -> (u32, u32, u32, u32) { // TODO hardcoding these to avoid floating point issues... urgh. :\ let ticks_per_second = 10; let ticks_per_minute = 60 * ticks_per_second; @@ -112,14 +112,14 @@ impl Tick { let hours = self.0 / ticks_per_hour; let mut remainder = self.0 % ticks_per_hour; let minutes = remainder / ticks_per_minute; - remainder = remainder % ticks_per_minute; + remainder %= ticks_per_minute; let seconds = remainder / ticks_per_second; - remainder = remainder % ticks_per_second; + remainder %= ticks_per_second; (hours, minutes, seconds, remainder) } - pub fn as_filename(&self) -> String { + pub fn as_filename(self) -> String { let (hours, minutes, seconds, remainder) = self.get_parts(); format!( "{0:02}h{1:02}m{2:02}.{3}s", diff --git a/sim/src/sim.rs b/sim/src/sim.rs index 166ae686ca..f70b038453 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -312,7 +312,7 @@ impl Sim { pub fn load_most_recent(&self) -> Result { let (_, load) = self .find_all_savestates() - .and_then(|mut list| list.pop().ok_or(io_error("empty directory")))?; + .and_then(|mut list| list.pop().ok_or_else(|| io_error("empty directory")))?; info!("Loading {}", load); abstutil::read_json(&load) } diff --git a/sim/src/spawn.rs b/sim/src/spawn.rs index 60c96f3c85..d5625dc8e0 100644 --- a/sim/src/spawn.rs +++ b/sim/src/spawn.rs @@ -188,7 +188,7 @@ impl Spawner { now, CreateCar { car, - trip: trip, + trip, owner: parked_car.owner, maybe_parked_car: Some(parked_car.clone()), vehicle: parked_car.vehicle.clone(), @@ -216,7 +216,7 @@ impl Spawner { now, CreateCar { car, - trip: trip, + trip, // TODO need a way to specify this in the scenario owner: None, maybe_parked_car: None, @@ -247,7 +247,7 @@ impl Spawner { now, CreateCar { car: vehicle.id, - trip: trip, + trip, owner: None, maybe_parked_car: None, vehicle: vehicle.clone(), diff --git a/sim/src/trips.rs b/sim/src/trips.rs index 80ceb179fb..f3894300d6 100644 --- a/sim/src/trips.rs +++ b/sim/src/trips.rs @@ -208,15 +208,12 @@ impl TripManager { spawned_at, finished_at: None, ped, - uses_car: legs - .iter() - .find(|l| match l { - TripLeg::Drive(_, _) => true, - TripLeg::DriveFromBorder(_, _) => true, - TripLeg::ServeBusRoute(_, _) => true, - _ => false, - }) - .is_some(), + uses_car: legs.iter().any(|l| match l { + TripLeg::Drive(_, _) => true, + TripLeg::DriveFromBorder(_, _) => true, + TripLeg::ServeBusRoute(_, _) => true, + _ => false, + }), legs: VecDeque::from(legs), }); id @@ -226,7 +223,7 @@ impl TripManager { pub fn get_trip_using_car(&self, car: CarID) -> Option { self.trips .iter() - .find(|t| t.legs.iter().find(|l| l.uses_car(car)).is_some()) + .find(|t| t.legs.iter().any(|l| l.uses_car(car))) .map(|t| t.id) } @@ -292,11 +289,11 @@ impl TripManager { // This will be None for parked cars pub fn agent_to_trip(&self, id: AgentID) -> Option { - self.active_trip_mode.get(&id).map(|id| *id) + self.active_trip_mode.get(&id).cloned() } pub fn get_active_trips(&self) -> Vec { - self.active_trip_mode.values().map(|id| *id).collect() + self.active_trip_mode.values().cloned().collect() } } diff --git a/sim/src/walking.rs b/sim/src/walking.rs index 0dfbf07b01..70badf207b 100644 --- a/sim/src/walking.rs +++ b/sim/src/walking.rs @@ -455,14 +455,7 @@ impl WalkingSimState { }); } Action::KeepPreparingBike => { - let state = self - .peds - .get(&id) - .unwrap() - .bike_parking - .as_ref() - .unwrap() - .clone(); + let state = self.peds[&id].bike_parking.as_ref().unwrap().clone(); if (now - state.started_at).as_time() >= TIME_TO_PREPARE_BIKE { if state.is_parking { // Now they'll start walking somewhere @@ -687,7 +680,7 @@ impl WalkingSimState { } pub fn ped_tooltip(&self, id: PedestrianID) -> Vec { - let p = self.peds.get(&id).unwrap(); + let p = &self.peds[&id]; vec![format!("{} is part of {}", p.id, p.trip)] } diff --git a/synthetic/src/main.rs b/synthetic/src/main.rs index 4444d27316..b219c46ef5 100644 --- a/synthetic/src/main.rs +++ b/synthetic/src/main.rs @@ -61,7 +61,7 @@ impl GUI for UI { State::LabelingBuilding(id, ref mut wizard) => { if let Some(label) = wizard.wrap(&mut input).input_string_prefilled( "Label the building", - self.model.get_b_label(id).unwrap_or("".to_string()), + self.model.get_b_label(id).unwrap_or_else(String::new), ) { self.model.set_b_label(id, label); self.state = State::Viewing; @@ -72,7 +72,7 @@ impl GUI for UI { State::LabelingRoad(pair, ref mut wizard) => { if let Some(label) = wizard.wrap(&mut input).input_string_prefilled( "Label this side of the road", - self.model.get_r_label(pair).unwrap_or("".to_string()), + self.model.get_r_label(pair).unwrap_or_else(String::new), ) { self.model.set_r_label(pair, label); self.state = State::Viewing; diff --git a/tests/src/geom.rs b/tests/src/geom.rs index ae3910c713..4149e8260a 100644 --- a/tests/src/geom.rs +++ b/tests/src/geom.rs @@ -2,6 +2,7 @@ use crate::runner::TestRunner; use geom::{line_intersection, Line, PolyLine, Pt2D}; use rand; +#[allow(clippy::unreadable_literal)] pub fn run(t: &mut TestRunner) { t.run_fast( "dist_along_horiz_line",