# False Positive S2583 Conditionally executed code should be reachable

**URL:** <https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608>\
**Category:** Report False-positive / False-negative...\
**Tags:** java\
**Created:** [November 17, 2020, 9:24pm UTC](https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608 "2020-11-17T21:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fred\_Robinson](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@Fred\_Robinson](https://community.sonarsource.com/u/Fred_Robinson)\
**Post date:** [November 17, 2020, 9:24pm UTC](https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608/1 "2020-11-17T21:24:48Z")

</div>

SonarQube 8.5 analyzing Java code with the SonarWay quality gate

The following code produces a false positive:

```auto
    public String findProspect() {
        String retVal = null;
        int attempts = 0;
        try {
            this.lastName = this.lastName.trim();
            final String fullOfferCode = getFullOfferCode(MediaType.DIRECT_MAIL);
            // get person from marketing database
            createPerson(fullOfferCode, MediaType.DIRECT_MAIL);
            // get campaign from marketing database
            createCampaign(fullOfferCode);
            retVal = "/term/quote.jsp?faces-redirect=true";
        } catch (final MalformedDataException ex) {
            logger.log(Level.INFO, "MalformedDataException: {0}", new Object[] {ex.getMessage()});
            attempts++;
        } catch (SystemUnavailableException | KBMSystemUnavailableException ex) {
            logger.log(Level.SEVERE, SYSTEM_UNAVAILABLE_EXCEPTION_0, new Object[] {ex.getMessage()});
            retVal = "/term/system-error.jsp?faces-redirect=true";
        } catch (final PersonNotFoundException ex) {
            logger.log(Level.INFO, ex.getMessage());
            attempts++;
        }
        // False Positive right here
        if (attempts > 0) {
            if (attempts < MAX_NUMBER_OF_ATTEMPTS) {
                final FacesMessage msg = new FacesMessage("Member Last Name or Personal Code not found. Please try again.");
                FacesContext.getCurrentInstance()
                        .addMessage("Member Last Name or Personal Code not found. Please try again.", msg);
            } else {
                retVal = "/term/access-error.jsp?faces-redirect=true";
            }
        }
        return retVal;
    }

```

(Yeah, it’s old crappy code that’s probably not used anymore.)

SQ does not seem to notice that `attempts` could be incremented in one or the other of the `catch` blocks. The exceptions could all be thrown by `createPerson()` and `createCampaign()`.

Fred Robinson

---

<div class="post-metadata">

**Author:** ![Margarita\_Nedzelska](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/margarita_nedzelska/32/47493_2.png) [@Margarita\_Nedzelska](https://community.sonarsource.com/u/Margarita_Nedzelska)\
**Post date:** [November 30, 2020, 4:39pm UTC](https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608/3 "2020-11-30T16:39:35Z")

</div>

Hi Fred!

Welcome to the community and thanks for your feedback.

Regarding your case, could you please clarify, which condition is reported as always evaluated to true.

`attempts > 0`  
or  
`attempts < MAX_NUMBER_OF_ATTEMPTS`

I tried your example, and I got that the second condition has an issue. But If you look at the code provided attentively, there is no way that the `attempts` variable will be greater than 1. Once you caught an exception you will never increment this local variable.

At the same moment, the first check doesn’t have any issue, because exception can be thrown and `attempts` variable can be greater than 0.

If you observe different behaviour on your side, please do not hesitate to come back here with more detailed explanation.

Kind regards,  
Margarita

---

<div class="post-metadata">

**Author:** ![Fred\_Robinson](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@Fred\_Robinson](https://community.sonarsource.com/u/Fred_Robinson)\
**Post date:** [November 30, 2020, 8:33pm UTC](https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608/4 "2020-11-30T20:33:12Z")

</div>

Here is a screenshot of the report form SonarQube.

 ![always_false](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/0/3/03afc88dac1cd853aba35f5435739356eee37ef5.png)

I read this as SonarQube saying that `attempts` will never be greater than zero.

I agree that the second condition will never be false, the way the method is written. It was supposed to permit up to 5 attempts to find a prospect, but setting `attempts` to zero every time renders this moot. `attempts` was originally a field of the object, but was only used in `findProspect()` (including setting it to zero), so SonarQube complained about that. On top of that, I can’t find any references to `findProspect()` in the code base (Java, JSP, JSF), leading me to think that this method is obsolete.

I’m more concerned by the SQ report.

Fred Robinson

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [December 18, 2020, 11:13am UTC](https://community.sonarsource.com/t/false-positive-s2583-conditionally-executed-code-should-be-reachable/34608/5 "2020-12-18T11:13:07Z")

</div>

Closed on December 18, 2020, 11:13am UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
