Skip to content

Commit

Permalink
lint: standardize formatting of let-else-return statements (#2297)
Browse files Browse the repository at this point in the history
`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)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2297

<!-- pr-link-docs:start -->
Docs preview: https://rerun.io/preview/c04c9b2/docs
<!-- pr-link-docs:end -->
  • Loading branch information
emilk authored Jun 1, 2023
1 parent 91d8b96 commit aa2565b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/re_data_ui/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions crates/re_viewer/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions scripts/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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'"

Expand Down Expand Up @@ -94,6 +102,7 @@ def test_lint_line() -> None:
"Wasm",
"num_instances",
"instances_count",
"let Some(foo) = bar else { return; };",
]

should_error = [
Expand All @@ -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:
Expand Down

0 comments on commit aa2565b

Please sign in to comment.