apache / apache/netbeans

"Suppress warning" action in record constructor produces invalid code

Open
#6,427 0 comments 0 reactions 0 assignees View on GitHub
hints Java kind:bug
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 19

### What happened

When applying a suggestion that would suppress a warning in a record constructor, the constructor code would be corrupted and invalid code would be left behind.

Given the following code :

```java
public record MyRecord(Object obj, E typed) {
public MyRecord {
typed = (E)obj;
}
}
```

The cast obviously raises an `unchecked` warning.
Trying to resolve it with the `Suppress Warning - unchecked` option it turns the code into the following invalid code :

```java
public record MyRecord(Object obj, E typed) {
@SuppressWarnings("unchecked")
public {
typed = (E)obj;
}
}
```

Instead, this is the expected code:

```diff
public record MyRecord(Object obj, E typed) {
@SuppressWarnings("unchecked")
- public {
+ public MyRecord {
typed = (E)obj;
}
}
```

### How to reproduce

_No response_

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Windows 11

### JDK

Temurin-17.0.8.1+1

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

_No response_

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the Suppress Warning action in the Java editor with the record example from the issue, then trace the code that generates the annotation and compact constructor. Done means the action produces a valid constructor retaining the MyRecord name and the unchecked warning is suppressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.