question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

option.get should not warn if scoped under isDefined

See original GitHub issue

there’s one use case where option.get is safe and reasonable:

def foo(bar: Option[String]) = {
    if (bar.isDefined) {
        val example = bar.get
        // do something with example
    }
    // we no longer care whether it was defined or not
}

The current implementation of this rule forces you use .getOrElse(somethingImpossible) in this context. That seems undesirable to me; things like if (false) and empty else { } are anti-patterns, so using getOrElse when you know the orElse is impossible should be one, too.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
sksamuelcommented, Jun 3, 2020

If you want to side effect only when an option is defined, use .foreach

On Wed, 3 Jun 2020 at 00:45, FFdhorkin notifications@github.com wrote:

On a related note:

I doubt you want to get into special handling for individual libraries, but Gatling’s API has an equivalent that is chainable off of ChainBuilder objects, though I’m not sure there’s a generic way to handle it:

chainOfCommands .doIf(bar.isDefined){ // do something with bar.get }

and

chainOfCommands .check( checkIf(bar.isDefined){ // do something with bar.get } )

and

chainOfCommands .doSwitch(bar.isDefined)( // parentheses, not curly braces! true -> bar.get, false -> ??? )

(there’s also doIfEquals, doIforElse, doIfEqualsOrElse, doSwitchOrElse, but they’re all pretty similar to the above. See https://gatling.io/docs/current/general/scenario/)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/sksamuel/sbt-scapegoat/issues/108#issuecomment-637968036, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGVUINJCOVYQWQVW57DRUXPODANCNFSM4NRK2NAA .

0reactions
mccartneycommented, Oct 3, 2020

Having said that, I believe the warning is a proper one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Problems with local variable scope. How to solve it?
When you try to access this variable inside mouseDown() method you are trying to access a local variable from within an anonymous inner...
Read more >
Variable scope, closure - The Modern JavaScript Tutorial
If a variable is not found anywhere, that's an error in strict mode (without use strict , an assignment to a non-existing variable...
Read more >
Warning Options (Using the GNU Compiler Collection (GCC))
When an unrecognized warning option is requested (e.g., -Wunknown-warning ), GCC emits a diagnostic stating that the option is not recognized.
Read more >
JSHint Options Reference
Warning This option has been deprecated and will be removed in the next major release of JSHint. JSHint is limiting its scope to...
Read more >
7.2. Warnings and sanity-checking
In Monoid instances declarations warn if any of the following conditions does not hold: If mappend is defined it must be canonical (i.e....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found