LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 40392 - readability-else-after-return bug when a variable is defined in the condition
Summary: readability-else-after-return bug when a variable is defined in the condition
Status: RESOLVED FIXED
Alias: None
Product: clang-tools-extra
Classification: Unclassified
Component: clang-tidy (show other bugs)
Version: unspecified
Hardware: PC Linux
: P enhancement
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-01-21 05:43 PST by Alexander Kornienko
Modified: 2019-01-21 08:28 PST (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alexander Kornienko 2019-01-21 05:43:46 PST
readability-else-after-return triggers for this pattern:
if (int x = ...) {
  ...
  return 0;
} else {
  // Do something with x
}

Removing the else clause would trigger a build error as x is no longer in scope

(translating a bug report from a user)
Comment 1 Jonas Toth 2019-01-21 07:27:32 PST
This should in principle apply to C++17 `if (auto Foo = bar(); Foo.method())`, right?
Comment 2 Alexander Kornienko 2019-01-21 08:28:58 PST
In this particular report the user was interested in C++11 only. But in principle - yes. I've submitted a patch to address this issue (r351751). Didn't check it for C++17 though.