Skip to content

Conversation

samueltardieu
Copy link
Member

@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

@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 -80
&& 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
Member 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
Member 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.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, those are fair points.

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.
@samueltardieu
Copy link
Member Author

Rebased

Copy link

Lintcheck changes for bd33a02

Lint Added Removed Changed
clippy::if_then_some_else_none 0 24 39

This comment will be updated if you push new changes

Comment on lines -73 to -80
&& 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.

Ok, those are fair points.

@y21 y21 added this pull request to the merge queue Aug 31, 2025
Merged via the queue into rust-lang:master with commit 3f74c96 Aug 31, 2025
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Aug 31, 2025
@samueltardieu samueltardieu deleted the issue-15005 branch August 31, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

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