From aa2565bfc0f71ed9e4b0446937f14d3f40ad843b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 1 Jun 2023 11:02:26 +0200 Subject: [PATCH] lint: standardize formatting of let-else-return statements (#2297) `cargo fmt` doesn't handle let-else statements, so let's try to keep a consistent style using a lint ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) PR Build Summary: https://build.rerun.io/pr/2297 Docs preview: https://rerun.io/preview/c04c9b2/docs --- crates/re_data_ui/src/image.rs | 2 +- crates/re_viewer/src/app.rs | 6 +++--- rerun_py/src/python_bridge.rs | 4 ++-- scripts/lint.py | 12 ++++++++++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crates/re_data_ui/src/image.rs b/crates/re_data_ui/src/image.rs index 75809168b913..77a53e1a760a 100644 --- a/crates/re_data_ui/src/image.rs +++ b/crates/re_data_ui/src/image.rs @@ -359,7 +359,7 @@ pub fn show_zoomed_image_region_area_outline( ) { use egui::{pos2, remap, Rect}; - let Some([height, width, _]) = tensor.image_height_width_channels() else {return;}; + let Some([height, width, _]) = tensor.image_height_width_channels() else { return; }; let width = width as f32; let height = height as f32; diff --git a/crates/re_viewer/src/app.rs b/crates/re_viewer/src/app.rs index 9c4aa0860b1a..2f133b0e3d95 100644 --- a/crates/re_viewer/src/app.rs +++ b/crates/re_viewer/src/app.rs @@ -434,11 +434,11 @@ impl App { } fn run_time_control_command(&mut self, command: TimeControlCommand) { - let Some(rec_id) = &self.state.selected_rec_id else {return;}; - let Some(rec_cfg) = self.state.recording_configs.get_mut(rec_id) else {return;}; + let Some(rec_id) = &self.state.selected_rec_id else { return; }; + let Some(rec_cfg) = self.state.recording_configs.get_mut(rec_id) else { return; }; let time_ctrl = &mut rec_cfg.time_ctrl; - let Some(log_db) = self.log_dbs.get(rec_id) else { return }; + let Some(log_db) = self.log_dbs.get(rec_id) else { return; }; let times_per_timeline = log_db.times_per_timeline(); match command { diff --git a/rerun_py/src/python_bridge.rs b/rerun_py/src/python_bridge.rs index f29024966fad..f2878ab94760 100644 --- a/rerun_py/src/python_bridge.rs +++ b/rerun_py/src/python_bridge.rs @@ -1171,7 +1171,7 @@ fn add_space_view( entity_paths: Vec<&str>, blueprint: Option<&PyRecordingStream>, ) { - let Some(blueprint) = get_blueprint_recording(blueprint) else { return }; + let Some(blueprint) = get_blueprint_recording(blueprint) else { return; }; let mut space_view = SpaceViewBlueprint::new( "Spatial".into(), @@ -1213,7 +1213,7 @@ fn add_space_view( #[pyfunction] fn set_auto_space_views(enabled: bool, blueprint: Option<&PyRecordingStream>) { - let Some(blueprint) = get_blueprint_recording(blueprint) else { return }; + let Some(blueprint) = get_blueprint_recording(blueprint) else { return; }; // TODO(jleibs) timeless? Something else? let timepoint = time(true, &blueprint); diff --git a/scripts/lint.py b/scripts/lint.py index 7b8abb19ce76..34fcb025cf5e 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -20,6 +20,7 @@ error_map_err_name = re.compile(r"map_err\(\|(\w+)\|") wasm_caps = re.compile(r"\bWASM\b") nb_prefix = re.compile(r"nb_") +else_return = re.compile(r"else\s*{\s*return;?\s*};") def lint_line(line: str) -> Optional[str]: @@ -63,6 +64,13 @@ def lint_line(line: str) -> Optional[str]: if name in ("e", "error"): return "Errors should be called 'err', '_err' or '_'" + m = re.search(else_return, line) + if m: + match = m.group(0) + if match != "else { return; };": + # Because cargo fmt doesn't handle let-else + return f"Use 'else {{ return; }};' instead of '{match}'" + if wasm_caps.search(line): return "WASM should be written 'Wasm'" @@ -94,6 +102,7 @@ def test_lint_line() -> None: "Wasm", "num_instances", "instances_count", + "let Some(foo) = bar else { return; };", ] should_error = [ @@ -112,6 +121,9 @@ def test_lint_line() -> None: "We use WASM in Rerun", "nb_instances", "inner_nb_instances", + "let Some(foo) = bar else {return;};", + "let Some(foo) = bar else {return};", + "let Some(foo) = bar else { return };", ] for line in should_pass: