Comments (4)
It could be potentially expanded to handled cases like an if/else
condition with one expression inside each branch. For now, I'm going to implement the lint for this simple case.
from rust-clippy.
A lot of people don't realize that
#[inline]
is also important when implementing simple trait functions like these, so that's why I'm calling it out explicitly.
Is this really still true today? As of recently rustc has started automatically adding #[inline]
to all "trivial" functions so it also works for non-LTO builds (this also broke compiler-explorer, which now requires #[inline(never)]
for functions to actually show up). IMHO any "trivial" function that rustc doesn't inline (and where it would be worth it) seems like a bug in its cost checker and it'd make more sense to me to just fix it in the MIR inliner instead of telling users to pollute their code with #[inline]
annotations all over the place
from rust-clippy.
I agree with you in priciple y21, but last I read about the auto-inlining in rustc it was extremely simple. Maybe it has improved though (and maybe I should open an issue on rustc instead).
Does anyone know the unit-tests for this is for rustc, so we can check which cases are actually handled?
from rust-clippy.
The PR that improved it is rust-lang/rust#116505. It adds the -Zcross-crate-inline-threshold
flag, by default it currently has the value 100.
That value is the max. number of MIR statements to consider "trivial" and to allow when considering if a function should be marked as inlineable. 100 statements should be enough for functions that simply return a field, construct a value, return an argument or do some small transformation to it, so it should indeed at least catch 3/4 cases mentioned.
The exact criteria are:
https://github.com/rust-lang/rust/blob/4a78c00e227124ff9d5ece2d493472e7325c87d3/compiler/rustc_mir_transform/src/cross_crate_inline.rs#L82-L85
Interestingly, that checker.calls == 0
condition does look like it would prevent the "does at most one function call" case from being inlined. I'm not sure if there's some technical reason for it I'm not aware of or if it could be tuned to return true for calls == 1 && statements == 0
specifically.
But anyway, if there are other trivial functions that aren't caught by this I do think an issue on the rust issue tracker would be better. Everyone would benefit from better heuristics in that area.
A clippy lint would duplicate what's already done in rustc but just with different heuristics that may or may not be better. There are some subtleties too that would need to be taken into account, which Rust's inliner does, such as being careful if a field access invokes Deref
code, or if any parameters are dropped and expensive Drop
code is executed at the end of the scope, ...
If the MIR cost checker something shouldn't be inlined, I don't think that clippy should say it either.
from rust-clippy.
Related Issues (20)
- ICE: index out of bounds when shadowing a function with a different number of arguments
- `ref_as_ptr` suggests using `ptr::from_ref(r)` where it should instead suggest `ptr::from_mut(r)` for `r: &mut T`.
- Suggested lint: avoid passing `&mut _` to `core::ptr::from_ref` HOT 1
- Add tuple swap algorithm detection to existing clippy::manual_swap
- internal compiler error: encountered incremental compilation error with mir_built(72773264b3289453-ebe898ff739d56f9) HOT 5
- Changing `bool` to `Result<(), ()>` in function return type leads to clippy warnings HOT 5
- Add lints against more manual integer ops where direct methods exist HOT 1
- Recommend `std::sync::LazyLock` over other lazy static options HOT 1
- Deny `static mut` declarations entirely HOT 1
- unnecessary_operation fires wrongly when extracting `!`
- Clippy crashes after using custom serde serializers for datetime HOT 1
- needless_pass_by_ref_mut triggers on mutable raw pointer
- Removing return not sugggested by `needless_return` when calling some chained string methods
- `needless_lifetimes` removes lifetimes that are actually necessary HOT 3
- `clippy::default_trait_access` can give incorrect suggestion
- `missing_inline_in_public_items` triggers for `impl From<Private> for Public`
- Possibly unintended use of unary `!` of non-`bool` type on left side of boolean expression/comparison
- Seemingly false positive of `doc_lazy_continuation` HOT 3
- Lint suggestion: `cfg!(not(...))` vs `!cfg!(...)`
- Lint suggestion: duplicate or redundant trait bounds HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-clippy.