dimforge / dimforge/nalgebra

Improve compiler warnings with #[must_use]

Open
#598 6 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
4.8k
Forks
565
PR merge metrics
No merged PRs in 30d

Description

Hello,

After porting my project to nalgebra, I spent way too much time debugging a black screen, which I eventually traced back to my following mistake:
```rust
view_matrix.prepend_translation(&(-camera));
```
This function returns a new matrix; the line is a no-op.

Enter [#[must_use]](https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute):
```rust
#[inline]
#[must_use = "Did you mean to use prepend_translation_mut()?"]
pub fn prepend_translation(
...
```
This turns a silent error into a helpful compiler warning:
```rust
| view_matrix.prepend_translation(&(-camera));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: #[warn(unused_must_use)] on by default
= note: Did you mean to use prepend_translation_mut()?
```

I propose to tag all functions that return a new object without side effects as #[must_use], optionally with a hint towards an in-place alternative.

This might also help with #534.

Is this something you'd consider an improvement? Shall I try to prepare a pull request, or would you rather go through your code yourself?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.