Skip to content
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

Add new case for question_mark lint #13772

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

DylanBulfin
Copy link

changelog: [question_mark]: suggests rewriting with question mark when returning an if-let-Some expression

fixes #13626

I think it's mostly done unless I've missed an edge case. I could use some pointers on formatting the suggestion though, I think my solution works but it's not very elegant.

@rustbot
Copy link
Collaborator

rustbot commented Dec 2, 2024

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @y21 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Dec 2, 2024
_: Span,
_: LocalDefId,
) {
let return_expr;
Copy link
Member

Choose a reason for hiding this comment

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

Can we make it more compact something like this

let return_expr = match body.value.kind {
      ExprKind::Block(block, _) => block.expr.or_else(|| {
          block.stmts.iter().last().and_then(|stmt| {
              if let StmtKind::Semi(expr) = &stmt.kind {
                  if let ExprKind::Ret(Some(ret)) = &expr.kind {
                      return Some(ret);
                  }
              }
              None
          })
      }),
      _ => None,
  };

if let Some(expr) = return_expr {
    check_if_let_some_as_return_val(cx, expr);
}

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the suggestion, this is much better

@DylanBulfin DylanBulfin force-pushed the return-if-let-some-else branch from f2d5663 to 5cc7eb6 Compare December 6, 2024 21:12
Comment on lines +486 to +489
&& if_else
.map(|e| eq_expr_value(cx, let_expr, peel_blocks(e)))
.filter(|e| *e)
.is_none()
Copy link
Member

Choose a reason for hiding this comment

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

What is this checking for? The is_early_return() checks that the else block is just None so this case seems unreachable to me? Unless you want to exclude specifically if let Some() = None but I'm not sure why

&& let ExprKind::Block(if_then_block, _) = if_then.kind
// Don't consider case where if-then branch has only one statement/expression
&& (if_then_block.stmts.len() >= 2 || if_then_block.stmts.len() == 1 && if_then_block.expr.is_some())
&& (is_early_return(sym::Option, cx, &if_block))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
&& (is_early_return(sym::Option, cx, &if_block))
&& is_early_return(sym::Option, cx, &if_block)


let body = snippet_with_applicability(cx, if_then.span, "..", &mut applicability);
let mut body_lines = body.lines().map(|line| &line[line.len().min(4)..]);
let (_, _) = (body_lines.next(), body_lines.next_back());
Copy link
Member

Choose a reason for hiding this comment

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

This seems like it could lead to broken suggestions if the whole (or parts of the) if let are on the same line as the if let header. I guess it's a bit of an unusual case considering it lints only when there are at least two statements and rustfmt would split that up into separate lines, but still seems suboptimal especially for a machine applicable suggestion.

I wonder if this could use the inside span of the if block's braces and clippy_utils::source::reindent_multiline?

_: Span,
_: LocalDefId,
) {
let return_expr = match body.value.kind {
Copy link
Member

Choose a reason for hiding this comment

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

This also needs some of the checks that are in check_expr. Notably, a check that this isn't a const fn and that the question_mark_used lint isn't enabled

};

let body = snippet_with_applicability(cx, if_then.span, "..", &mut applicability);
let mut body_lines = body.lines().map(|line| &line[line.len().min(4)..]);
Copy link
Member

Choose a reason for hiding this comment

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

Instead of hardcoding 4, this might be a good use case for clippy_utils::source::indent_of(expr.span)

if_then,
if_else,
)
&& let ExprKind::Block(if_then_block, _) = if_then.kind
Copy link
Member

Choose a reason for hiding this comment

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

There should be a check that this code isn't from an (external) macro expansion (can use in_external_macro)

.is_none()
{
let mut applicability = Applicability::MachineApplicable;
let receiver_str = snippet_with_applicability(cx, let_expr.span, "..", &mut applicability);
Copy link
Member

Choose a reason for hiding this comment

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

Should be snippet_with_context with expr.span.ctxt() for the syntax context, and a test case would be good where the if let scrutinee is a macro invocation:

macro_rules! m { () => { Some(()) } }

fn foo() -> Option<()> {
  if let Some(v) = m!() {
    Some(())
  } else {
    None
  }
}

currently the suggestion contains the macro expansion directly (if let Some(v) = Some(()))

Comment on lines +592 to +593
// Defining check_fn instead of using check_body to avoid accidentally triggering on
// const expressions, not sure if this is necessary
Copy link
Member

Choose a reason for hiding this comment

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

Makes sense to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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.

Simplify if-let-Some-else-None using question mark operator
4 participants