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

[SR-978] Warn on do {} with only one empty catch {} block #43590

Open
jckarter opened this issue Mar 18, 2016 · 9 comments
Open

[SR-978] Warn on do {} with only one empty catch {} block #43590

jckarter opened this issue Mar 18, 2016 · 9 comments
Labels
compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation do catch Feature → statements: 'do' statements good first issue Good for newcomers improvement missing warning Bug: Missing warning statements Feature: statements swift 5.9

Comments

@jckarter
Copy link
Contributor

Previous ID SR-978
Radar None
Original Reporter @jckarter
Type Improvement
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, StarterBug
Assignee MultiColourPixel (JIRA)
Priority Medium

md5: d5f41243d2aaf067f1ecd57198c44970

Issue Description:

Eric Knapp caught some sample code in the wild unwisely using an empty catch block to swallow errors:

https://twitter.com/ejknapp/status/710589235464523776

We should warn on this to encourage people to use try! to trap on unexpected errors instead.

@belkadan
Copy link
Contributor

How do you silence the warning?

@jckarter
Copy link
Contributor Author

Morally, if you don't expect any errors to happen, you should use try! to assert that. Practically, if you really want to throw away errors, there's _ = try?.

@belkadan
Copy link
Contributor

Yes, but what about "I want to catch this particular error, but ignore any others?" ("I want to ignore this particular error" is easy enough to special-case.)

@jckarter
Copy link
Contributor Author

I see what you're saying. IMO, if you do that, we should let it pass:

do {
  try something()
} catch Foo.Error {
  handleFooError()
} catch {
  /* do nothing */
}

IMO, it's only the case where you do nothing more than do { ... } catch {} that deserves a warning.

@swift-ci
Copy link
Contributor

Comment by Josef Willsher (JIRA)

I’m happy to implement this. How should we handle cases where the do block contains more than 1 expression—we can't issue a fixit here, but should I still emit a warning?

@jckarter
Copy link
Contributor Author

If the do block contains multiple statements, you should still be able to change every try to try! or try? inside it.

@swift-ci
Copy link
Contributor

Comment by Josef Willsher (JIRA)

try? has different semantics to try though — you won't be able to just interchange them in an expression like foo(a: try getA()).

Are you sure its not too much compiler gymnastics for a fixit to change a whole scope's try expressions to the trapping try!? I think it is reasonable to do for a single expression, but if the do block contains a significant amount of code, surely the better alternative is to get the user to rethink it themselves?

I already have it emitting a warning for a single, empty, unguarded catch block, and I’m happy to implement the fixit either way.

@jckarter
Copy link
Contributor Author

Sure, it might be reasonable to just warn without fixits if the `do` block is too complex.

@belkadan
Copy link
Contributor

Resetting assignee for all Starter Bugs not modified since 2018.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added missing warning Bug: Missing warning do catch Feature → statements: 'do' statements diagnostics QoI Bug: Diagnostics Quality of Implementation statements Feature: statements swift 5.9 labels Sep 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler itself diagnostics QoI Bug: Diagnostics Quality of Implementation do catch Feature → statements: 'do' statements good first issue Good for newcomers improvement missing warning Bug: Missing warning statements Feature: statements swift 5.9
Projects
None yet
Development

No branches or pull requests

4 participants