Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static analyzes always show warning in every case of @Nullable field access. #3255

Closed
Burtsev-Alexey opened this issue Aug 25, 2023 · 8 comments · Fixed by eclipse-jdtls/eclipse.jdt.ls#2822
Assignees

Comments

@Burtsev-Alexey
Copy link

Burtsev-Alexey commented Aug 25, 2023

[provide a description of the issue]

Environment
  • Operating System: Windows
  • JDK version: 20
  • Visual Studio Code version: 1.81.1
  • Java extension version: 1.21.0
Steps To Reproduce

See screenshot.

Current Result

Always show warning "Potential null pointer access"

Expected Result

Some null check code exists that is considered as null check done, so warning is not shown. I understand that t is hard to analyze complex null check scenarios, but I'll be happy if simple code like "if (obj!=null) {obj.foo()}" will not show warning.

Additional Informations

2023-08-25_17-15-53

Java language server log contains no errors or warnings:
!ENTRY org.eclipse.jdt.ls.core 1 0 2023-08-25 17:17:34.804
!MESSAGE 4 problems reported for /NullCheck.java

@fbricon
Copy link
Collaborator

fbricon commented Aug 25, 2023

looks like an upstream JDT bug. Can you check if the same problem exists in Eclipse IDE and maybe report a bug in https://github.com/eclipse-jdt/eclipse.jdt.core?

@Burtsev-Alexey
Copy link
Author

I have tried "NetBeans IDE 18" it's behavior is same as VSCode.
NetBeans IDE 18 always show warnings on "Nullable" access.

@fbricon
Copy link
Collaborator

fbricon commented Aug 25, 2023

Intellij IDEA performs more accurate null-analysis. Eclipse JDT should work better. The fact Netbeans provide the same broken behavior is irrelevant.

@Burtsev-Alexey
Copy link
Author

I have created issue at eclipse.jdt.core
eclipse-jdt/eclipse.jdt.core#1311

@Burtsev-Alexey
Copy link
Author

Intellij IDEA performs more accurate null-analysis. Eclipse JDT should work better. The fact Netbeans provide the same broken behavior is irrelevant.

I'm sure you are right. Thought I'm a developer, I use IDEs as end user, and dont fully understand how they do their job.

@Burtsev-Alexey Burtsev-Alexey changed the title @Nullable static analyzes always show warning in every case of null check Static analyzes always show warning in every case of @Nullable field access. Aug 28, 2023
@Burtsev-Alexey
Copy link
Author

Burtsev-Alexey commented Aug 28, 2023

We have figured out this problem with jdt.core team eclipse-jdt/eclipse.jdt.core#1311

Indeed this is a bug in jdt.core and they are going to fix it. But this alone is not enough to make it work as expected in VS Code. By default jdt.core will still show warning, you need to set an "Enable syntactic analysis" option as described in their documentation:
image

@fbricon Is there a way to configure this in VSCode Java extension now? If no, can you implement it?
I think this option should be on by default because, 99.9% developers would expect code if (obj.field!=null) obj.field.toString() - to not show NPE warning.

@fbricon
Copy link
Collaborator

fbricon commented Aug 28, 2023

In your project, create a .vscode/jdt.core.prefs file (the name doesn't matter actually), containing:

org.eclipse.jdt.core.compiler.problem.syntacticNullAnalysisForFields=enabled

then, in your project's .vscode/settings.json add a reference to that file:

"java.settings.url": ".vscode/jdt.core.prefs"

You can learn more about exposing Eclipse JDT settings in vscode in https://github.com/redhat-developer/vscode-java/wiki/Settings-Global-Preferences

@fbricon
Copy link
Collaborator

fbricon commented Aug 28, 2023

@rgrunber @hopehadfield I agree, we should turn that preference on by default in JDT.LS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants