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

Missing "unused imports" warnings when there's a compilation error #621

Closed
brookhong opened this issue Apr 12, 2018 · 9 comments
Closed

Comments

@brookhong
Copy link

textDocument/publishDiagnostics does not return unused imports in java.

Any chance to support this?

@fbricon
Copy link
Contributor

fbricon commented Apr 12, 2018

Unused imports are absolutely reported. Except when a single Java file with no known classpath is opened, what we call a standalone java file.
If your project is Maven or Gradle-built, you should see unused import diagnostics, if you don't please provide a sample project reproducing the issue.

@brookhong
Copy link
Author

This is weird.

I tried several other projects, unused imports are reported. But only for this one(I put it on github just now), it does not work.

I used in vim with YouCompleteMe, please see these two imports:

import java.util.ArrayList;
import java.util.List;

image

And here is the log from YCM:

2018-04-12 22:29:36,791 - DEBUG - RX: Received message: b'{"jsonrpc":"2.0","method":"textDocument/publishDiagnostics","params":{"uri":"file:///works/TestUnusedImports/src/main/java/com/halodu/HomeController.java","diagnostics":[{"range":{"start":{"line":23,"character":35},"end":{"line":23,"character":51}},"severity":2,"code":"570425421","source":"Java","message":"The value of the field HomeController.datastoreService is not used"},{"range":{"start":{"line":37,"character":8},"end":{"line":37,"character":14}},"severity":1,"code":"570425394","source":"Java","message":"LOGGER cannot be resolved"}]}}'

@fbricon
Copy link
Contributor

fbricon commented Apr 12, 2018

The same project opens fine and report unused imports in vscode. Maybe @puremourning can help here?

@micbou
Copy link

micbou commented Apr 12, 2018

Tried with the latest version of jdt.ls (YCM is currently on 0.15.0) and there are still no errors about unused imports. However, the command to organize imports is removing the unused imports so jdt.ls clearly knows that the imports are not used.

The same project opens fine and report unused imports in vscode.

Strange, I don't see any diagnostics about unused imports with vscode-java 0.22.0.

@brookhong
Copy link
Author

I don't think YCM causes this, because I looked into the YCM log(which I had attached above), and here is the formatted received message from jdt.ls by ycmd.

{
    "jsonrpc": "2.0",
    "method": "textDocument/publishDiagnostics",
    "params": {
        "diagnostics": [
            {
                "code": "570425421",
                "message": "The value of the field HomeController.datastoreService is not used",
                "range": {
                    "end": {
                        "character": 51,
                        "line": 23
                    },
                    "start": {
                        "character": 35,
                        "line": 23
                    }
                },
                "severity": 2,
                "source": "Java"
            },
            {
                "code": "570425394",
                "message": "LOGGER cannot be resolved",
                "range": {
                    "end": {
                        "character": 14,
                        "line": 37
                    },
                    "start": {
                        "character": 8,
                        "line": 37
                    }
                },
                "severity": 1,
                "source": "Java"
            }
        ],
        "uri": "file:///works/TestUnusedImports/src/main/java/com/halodu/HomeController.java"
    }
}

@brookhong
Copy link
Author

I also tried with latest eclipse.jdt.ls(dev build) with YCM, the message for textDocument/publishDiagnostics is same as above.

@brookhong
Copy link
Author

I did some more investigation, and found that there might be some issue of eclipse.jdt.core.

A severity 1 error (LOGGER cannot be resolved) at line 38 is blocking two severity 2 (The import java.util.ArrayList is never used, The import java.util.List is never used) from being reported.

image

The same problem happens in eclipse.

image

diagnostics before line 38 commented out

{
    "jsonrpc": "2.0",
    "method": "textDocument/publishDiagnostics",
    "params": {
        "diagnostics": [
            {
                "code": "570425421",
                "message": "The value of the field HomeController.datastoreService is not used",
                "range": {
                    "end": {
                        "character": 51,
                        "line": 23
                    },
                    "start": {
                        "character": 35,
                        "line": 23
                    }
                },
                "severity": 2,
                "source": "Java"
            },
            {
                "code": "570425394",
                "message": "LOGGER cannot be resolved",
                "range": {
                    "end": {
                        "character": 14,
                        "line": 37
                    },
                    "start": {
                        "character": 8,
                        "line": 37
                    }
                },
                "severity": 1,
                "source": "Java"
            }
        ],
        "uri": "file:///works/TestUnusedImports/src/main/java/com/halodu/HomeController.java"
    }
}

diagnostics after line 38 commented out

{
    "jsonrpc": "2.0",
    "method": "textDocument/publishDiagnostics",
    "params": {
        "diagnostics": [
            {
                "code": "268435844",
                "message": "The import java.util.ArrayList is never used",
                "range": {
                    "end": {
                        "character": 26,
                        "line": 2
                    },
                    "start": {
                        "character": 7,
                        "line": 2
                    }
                },
                "severity": 2,
                "source": "Java"
            },
            {
                "code": "268435844",
                "message": "The import java.util.List is never used",
                "range": {
                    "end": {
                        "character": 21,
                        "line": 3
                    },
                    "start": {
                        "character": 7,
                        "line": 3
                    }
                },
                "severity": 2,
                "source": "Java"
            },
            {
                "code": "570425421",
                "message": "The value of the field HomeController.datastoreService is not used",
                "range": {
                    "end": {
                        "character": 51,
                        "line": 23
                    },
                    "start": {
                        "character": 35,
                        "line": 23
                    }
                },
                "severity": 2,
                "source": "Java"
            }
        ],
        "uri": "file:///works/TestUnusedImports/src/main/java/com/halodu/HomeController.java"
    }
}

@brookhong
Copy link
Author

brookhong commented Apr 16, 2018

I reported the bug for eclipse.jdt.core -- https://bugs.eclipse.org/bugs/show_bug.cgi?id=533592
Feel free to close this if you'd like.

@fbricon fbricon changed the title unused imports in java Missing "unused imports" warnings when there's a compilation error Jun 13, 2018
@fbricon fbricon added not a bug and removed bug labels Sep 9, 2021
@fbricon
Copy link
Contributor

fbricon commented Sep 9, 2021

Closing, according to https://bugs.eclipse.org/bugs/show_bug.cgi?id=533592#c2:

This is by design: as long as resolve errors are present, unused imports warning would in general not be reliable. So it's better to focus on definite and more severe errors first.

@fbricon fbricon closed this as completed Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants