4

Add size_of_ref lint by lukaslueg · Pull Request #10098 · rust-lang/rust-clippy...

 1 year ago
source link: https://github.com/rust-lang/rust-clippy/pull/10098
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Contributor

@lukaslueg lukaslueg commented Dec 17, 2022

edited by xFrednet

This addresses #9995, which is likely raising a valid point about std::mem::size_of_val(): It's very easy to use double-references as the argument, which the function will happily accept and give back the size of the reference, not the size of the value behind the reference. In the worst case, if the value matches the programmer's expectation, this seems to work, while in fact, everything will go horribly wrong e.g. on a different platform.

The size of a &T is independent of what T is, and people might want to use std::mem::size_of_val() to actually get the size of any reference (e.g. via &&()). I would rather suggest that this is always bad behavior, though (instead, and). I, therefore, put this lint into correctness.

Since the problem is usually easily fixed by removing extra &, I went light on suggesting code.


changelog: New lint: [size_of_ref]
#10098


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK