Skip to content

Remove all unwraps from project and follow stricter linting rules #145

@qarmin

Description

@qarmin

The project should use Clippy, including non-default lints that catch potential problems — currently clippy is not enforced, which means several interesting potential bugs or optimization opportunities are being missed.

I recommend adding the following [lints] subsection at the end of your Cargo.toml file (these are fairly basic):

[lints]
clippy.dbg_macro = "warn"
clippy.branches_sharing_code = "warn"
clippy.needless_pass_by_value = "warn"
clippy.needless_raw_strings = "warn"
clippy.string_slice = "warn"
clippy.unwrap_used = "warn"
clippy.unused_self = "warn"

Then, fix each issue one by one by running:

cargo clippy

For example, the unwrap_used lint detects unwrap calls — these should be replaced with expect(...) when the failure is critical and there’s no reasonable recovery path or proper error handling (Err, ?) otherwise.

Examples of warnings:

warning: used `unwrap()` on an `Option` value
   --> src/formatter.rs:362:37
    |
362 |                     let last_node = m.captures.last().unwrap().node;
    |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: if this value is `None`, it will panic
    = help: consider using `expect()` to provide a better panic message
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
warning: indexing into a string may panic if the index is within a UTF-8 character
   --> src/formatter.rs:297:57
    |
297 |                 calculate_end_position(start_position, &self.content[start_byte..old_end_byte]);
    |                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#string_slice

This are not hypothetical issues.
Some files causes crashes:

 thread 'main' (206859) panicked at src/formatter.rs:603:79:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/b925a865e2c9a0aefe5a2877863cb4df796f2eaf/library/std/src/panicking.rs:698:5
   1: core::panicking::panic_fmt
             at /rustc/b925a865e2c9a0aefe5a2877863cb4df796f2eaf/library/core/src/panicking.rs:80:14
   2: core::panicking::panic
             at /rustc/b925a865e2c9a0aefe5a2877863cb4df796f2eaf/library/core/src/panicking.rs:150:5
   3: core::option::unwrap_failed
             at /rustc/b925a865e2c9a0aefe5a2877863cb4df796f2eaf/library/core/src/option.rs:2169:5
   4: core::option::Option<T>::unwrap
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:1010:21
   5: gdscript_formatter::formatter::GdTree::move_annotations::{{closure}}
             at ./GDScript-formatter-main/src/formatter.rs:603:79
   6: <core::iter::adapters::map_while::MapWhile<I,P> as core::iter::traits::iterator::Iterator>::next
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/adapters/map_while.rs:45:9
   7: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter_nested.rs:24:41
   8: <alloc::vec::Vec<T> as alloc::vec::spec_from_iter::SpecFromIter<T,I>>::from_iter
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/spec_from_iter.rs:33:9
   9: <alloc::vec::Vec<T> as core::iter::traits::collect::FromIterator<T>>::from_iter
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:3689:9
  10: core::iter::traits::iterator::Iterator::collect
             at /home/runner/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:2028:9
  11: gdscript_formatter::formatter::GdTree::move_annotations
             at ./GDScript-formatter-main/src/formatter.rs:613:26
  12: gdscript_formatter::formatter::GdTree::postprocess
             at ./GDScript-formatter-main/src/formatter.rs:529:14
  13: gdscript_formatter::formatter::Formatter::new
             at ./GDScript-formatter-main/src/formatter.rs:56:20
  14: gdscript_formatter::formatter::format_gdscript_with_config
             at ./GDScript-formatter-main/src/formatter.rs:33:25
  15: gdscript_formatter::main::{{closure}}
             at ./GDScript-formatter-main/src/main.rs:241:17

File - compressed.zip
Command timeout -v 40 gdscript-formatter TEST___FILE.gd

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions