Skip to content

Get the block content from the proper context #15014

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

samueltardieu
Copy link
Contributor

@samueltardieu samueltardieu commented Jun 8, 2025

The first statement of the block might have been in a different context from the expression. Walk up to the right context to get bounds properly.

Also, switch to snippet_with_applicability() since we know that we are in the right context already.

changelog: [if_then_some_else_none]: emit well-formed suggestion, and do not lint inside macros

Fixes #15005

The first statement of the block might have been in a different context
from the expression. Walk up to the right context to get bounds
properly.

Also, switch to `snippet_with_applicability()` since we know that we are
in the right context already.
@rustbot
Copy link
Collaborator

rustbot commented Jun 8, 2025

r? @y21

rustbot has assigned @y21.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 8, 2025
Comment on lines -73 to -79
&& let ctxt = expr.span.ctxt()
&& then_expr.span.ctxt() == ctxt
&& !expr.span.from_expansion()
&& !then_expr.span.from_expansion()
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
&& !is_else_clause(cx.tcx, expr)
&& !is_in_const_context(cx)
&& !expr.span.in_external_macro(cx.sess().source_map())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for why we don't want to lint code from within local macros anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Is there a context in which I can get the non-expanded code? If the if … { Some(…) } else { None } is in the macro, using the context of the if expression is not enough to get non-expanded code for the condition or the content of the Some. How can I retrieve it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another reason would be that we sometimes suggest .then(|| …) and sometimes .then_some(). Here, in a macro, we would have to suggest .then(|| …) because we won't check every call for the absence of side effects.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

if-then-some-else-none does not account for extra exprs before the Some(...)
3 participants