-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Created an assist for inlining a function's body into its caller #7131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The tests currently fail because the replaced code isn't formatted correctly. Is it possible to ask rust-analyzer to automatically format the replacement code, or should I just update the doc-test to have each bound parameter on its own line? fn add(a: u32, b: u32) -> u32 { a + b }
fn main() {
- let x = { let a = 1; let b = 2; a + b };
+ let x = {
+ let a = 1;
+ let b = 2;
+ a + b
+ };
} |
Some further improvements:
|
The trivial example from the original issue is now expanded as desired. Is someone able to review the PR and let me know their thoughts? In particular, I'd like to know whether I should implement the extensions/tweaks from my previous comment. @Nadrieril, do you think it'd be worth trying to "inline" the block we replace the original function call with? At the moment it'll generate something like this: fn add(a: u32, b: u32) -> u32 { a + b }
fn main() {
let x = {
let a = 1;
let b = 2;
a + b
};
} Which can be simplified down to |
Yay, thanks for implementing this!
I would find that useful but not a deal-breaker if you don't want to risk shadowing.
That's easy enough to do by hand with the "inline variable" code action, so I'd rather do it myself if I want.
Definitely! Also trait methods if possible. |
@Nadrieril, can you give me an example of what inlining a method call or trait method call would look like? And what happens if the call being inlined is part of a method chain (e.g. the |
I dunno, the only difference with functions is that struct Bar(Baz);
impl Bar {
fn bar(&self, n: usize) -> &Baz {
self.0.whatever(n)
}
}
fn main() {
let x = Foo;
x.foo().bar<|>(0).baz()
} ===> // Same `Bar`...
fn main() {
let x = Foo;
{
let _self = &x.foo();
let n = 0;
(_self.0.whatever(n)).baz()
}
} I don't know how to name the dummy |
not really, see #4182. There's For tweaks, I suggest starting reasonably simple -- the full impl would be pretty complex. If you want to do "only one usage" trick, see the "remove unused parameter" assist for the search infra. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, lgtm!
huh, not sure why ci fails... |
bors try |
tryBuild failed: |
Thanks for the review, @matklad! Would you like me to squash everything into a single commit before merging? I'm not sure why the Rust Cross job is failing. It seems to think that calling |
We don't ask contributors to squash, but we don't squash on merge either. In this case, your changes are pretty well-contained, so it would make sense to have a single commit. You might also want to do that because...
I'm not sure either, but see #7115. You should probably rebase over the current |
8de5be4
to
a536f06
Compare
a536f06
to
7b4b4ef
Compare
@lnicola thanks, I've rebased and CI is happy now. |
statements.push(make::let_stmt(pattern, Some(value)).into()); | ||
} | ||
|
||
statements.extend(body.statements()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is ok for the initial impl, but it'll erase formatting, the prepend_stmt
solution would better preserve original code.
bors r+ |
This introduces an
inline_function
assist which will convert code like this:Into something like this:
Fixes #6863.