# \[Java:S1905\] Necessary casts are reported as unnecessary

**URL:** <https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643>\
**Category:** Report False-positive / False-negative...\
**Tags:** sonarqube, java\
**Created:** [October 29, 2020, 9:29am UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643 "2020-10-29T09:29:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfroehlich](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mfroehlich](https://community.sonarsource.com/u/mfroehlich)\
**Post date:** [October 29, 2020, 9:29am UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/1 "2020-10-29T09:29:35Z")

</div>

I don’t know, if this example hits it exactly. But let’s try.

In Eclipse (SonarLint) this line is NOT complained about. But Qube does. The project is bound to Qube.

```
public void foo()
{
    Runnable r = () ->
    {
        for ( X x : xs )
       {
           String s = (String) x.getObject(); // FP
       }
    };
}

```

The X.getObject() method returns an Object. Hence the cast to String is necessary.

There are some other cases, where this is wrong. All of them seem to be inside a lambda expression. Don’t know, if that matters.

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [November 5, 2020, 4:15pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/2 "2020-11-05T16:15:48Z")

</div>

Hello @mfroehlich,

It sounds strange that you have an issue in SonarLint but not in SonarQube, can you check your project configuration? Especially `sonar.java.binaries`.

I did not manage to reproduce the problem with your code, but with a somehow similar one:

```auto
void foo() {
  f(arg -> {
    String s = (String) arg.get(0)[1]; // FP
    return null;
  });
}
void f(Function<List<Object[]>, Object> function) { }

```

The good news is that we slightly reworked this rule in version 6.9 of the Java analyzer and that the FP is no longer present.

At this point, I have good hope that the issue you are facing will disappear when upgrading, but without any other reproducer, I can’t guarantee that.

---

<div class="post-metadata">

**Author:** ![mfroehlich](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mfroehlich](https://community.sonarsource.com/u/mfroehlich)\
**Post date:** [November 6, 2020, 8:56am UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/3 "2020-11-06T08:56:51Z")

</div>

That’s good enough for me. Thanks a lot.

Please notice, that it was the other way round here. I had an issue in Qube, but not in Lint.

---

<div class="post-metadata">

**Author:** ![mfroehlich](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mfroehlich](https://community.sonarsource.com/u/mfroehlich)\
**Post date:** [November 12, 2020, 10:42am UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/4 "2020-11-12T10:42:58Z")

</div>

I have another FP, that still exists in SonarLint 3.5.

```
public class C
{
    public void foo( BinaryOperator< String > op )
    {
    }
    
    public void foo( Function< String[], String > f )
    {
    }
}

new C().foo( (Function< String[], String >) this::matchingMethod );

```

The Java compiler (Eclipse) wants a cast here. And SonarLint marks it as unnecessary (which it actually is, but not accepted by Java (11) compiler).

I guess, it’s the same for any overloaded method with function pointer parameter like this.

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [November 12, 2020, 1:52pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/6 "2020-11-12T13:52:12Z")

</div>

What is the signature of `this::matchingMethod`?

I manage to reproduce a FP with this code:

```auto
  private static String matchingMethod(String s, String s1) {
    return "";
  }

  private static String matchingMethod(String[] strings) {
    return "";
  }

```

And same observation as the first post, we reworked ambiguous method reference ([SONARJAVA-3468](https://jira.sonarsource.com/browse/SONARJAVA-3468)), this issue is not here anymore in the latest version.

---

<div class="post-metadata">

**Author:** ![mfroehlich](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mfroehlich](https://community.sonarsource.com/u/mfroehlich)\
**Post date:** [November 12, 2020, 2:15pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/7 "2020-11-12T14:15:07Z")

</div>

> [@Quentin](#):
>
> What is the signature of `this::matchingMethod` ?

```
public < Value > Value matchingMethod( Value... args );

```

If this is also solved in the future release, that’s fine. Thanks,

---

<div class="post-metadata">

**Author:** ![Quentin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/quentin/32/74716_2.png) [@Quentin](https://community.sonarsource.com/u/Quentin)\
**Post date:** [November 12, 2020, 2:39pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/8 "2020-11-12T14:39:27Z")

</div>

Thanks for the complete reproducer, I see the problem now.

I’m afraid this situation is still not correctly supported, ticket to improve this further: [SONARJAVA-3615](https://jira.sonarsource.com/browse/SONARJAVA-3615).

Best,  
Quentin

---

<div class="post-metadata">

**Author:** ![mfroehlich](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@mfroehlich](https://community.sonarsource.com/u/mfroehlich)\
**Post date:** [November 12, 2020, 5:16pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/10 "2020-11-12T17:16:01Z")

</div>

Great, thanks.

---

<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:** [November 19, 2020, 5:16pm UTC](https://community.sonarsource.com/t/java-s1905-necessary-casts-are-reported-as-unnecessary/33643/11 "2020-11-19T17:16:04Z")

</div>

Closed on November 19, 2020, 5:16pm UTC

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