This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 253195 - Incorrect "remove redundant cast" hint for lambdas
Summary: Incorrect "remove redundant cast" hint for lambdas
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.1
Hardware: PC Windows 7
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-26 03:29 UTC by _ gtzabari
Modified: 2015-08-05 01:20 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description _ gtzabari 2015-06-26 03:29:09 UTC
Product Version: NetBeans IDE Dev (Build 201506240001)
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b14
System: Windows 7 version 6.1 running on amd64; Cp1252; en_CA (nb)
User directory: C:\Users\Gili\AppData\Roaming\NetBeans\dev
Cache directory: C:\Users\Gili\AppData\Local\NetBeans\Cache\dev

Given:

---
import com.codahale.metrics.MetricRegistry;

[...]
MetricRegistry registry = getMetrics();
registry.register("name", (Gauge<Double>) () -> 5.0);
---

Netbeans annotates the cast to "Gauge<Double>" with a "remove redundant cast" hint but removing the cast results in a compiler error "Metric is not a functional interface".

To recap:
1. The code is invoking a method that takes a Metric argument.
2. Gauge extends Metric, but Gauge *is* a functional interface whereas Metric is not.

Expected behavior: The hint should not be offered for lambdas if removing the cast results in a type which is not a functional interface.
Comment 1 Svata Dedic 2015-07-31 06:24:39 UTC
God knows what boxing / unboxing / whatever will break. I changed the final check so it attempts to actually parse+attribute the exact proposed code for the method call and report unnecessary cast if and only if the code does not produce an error and resolves to the same method.

Fixed in jet-main#df1a8506223a
Comment 2 Quality Engineering 2015-08-05 01:20:47 UTC
Integrated into 'main-silver', will be available in build *201508050002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/df1a8506223a
User: Svata Dedic <sdedic@netbeans.org>
Log: #253195: suggested code better checked for errors and ambiguity