启用 -Xlint 以供 VSCode Java 编译使用。

huangapple go评论78阅读模式
英文:

Enable -Xlint for VSCode Java Compiling

问题

The Java extensions for VSCode在捕获大多数警告方面表现出色,但最近在我的项目中发现了一些VSCode没有报告的警告,而我是通过使用 -Xlint 标志进行编译而发现的。

以这个简短的示例为例:

public class Test
{
    public static void main(String[] args)
    {
        int x = 3;
        int y = (int)x;

        System.out.println(y);
    }
}

这段代码在VSCode中没有任何警告,但如果你使用 javac -Xlint Test.java 进行编译,将会得到以下警告:

Test.java:6: warning: [cast] redundant cast to int
        int y = (int)x;
                ^      
1 warning

我希望的是以与其他警告相同的方式在VSCode中报告这些警告(以黄色波浪线的方式进行突出显示,您可以将鼠标悬停在上面)。我尝试过更改默认的 javac 编译器参数,但未成功找到配置选项(如果存在的话)。

上述警告只是VSCode似乎不会报告的一个示例。我还通过使用 -Xlint 标志发现了其他警告,例如应标记为 transient 的字段未被标记。

编辑: 我已经为 RedHat 的 VSCode Java 扩展提出了一个功能请求 这里

英文:

The Java extensions for VSCode do an excellent job of catching most warnings, but recently I came across several warnings in my project that VSCode was not reporting but which I found through compiling using the -Xlint flag.

Take this SSCCE for example:

public class Test
{
    public static void main(String[] args)
    {
        int x = 3;
        int y = (int)x;

        System.out.println(y);
    }
}

This code runs perfectly fine with no warnings in VSCode, but if you compile it with javac -Xlint Test.java, you will get this:

Test.java:6: warning: [cast] redundant cast to int
        int y = (int)x;
                ^      
1 warning

What I would like is a way of having these warnings get reported in VSCode just like other warnings (where they are highlighted with yellow squiggles that you can mouse over). I have tried, unsuccessfully, to see if there was a way of changing the default javac compiler arguments but have not found the configuration option (if it exists).

And the above warning is just a single example of things VSCode does not appear to report. I have found other warnings through using the -Xlint flag as well, such as fields that should be marked transient that are not.

Edit: I have opened a feature request for RedHat's VSCode Java extension here.

答案1

得分: 1

SonarLint 可能是你需要的东西。

> SonarLint 是一个易于使用的扩展,可以在编码时帮助你找到并修复错误和安全问题。该扩展在后台运行,就像拼写检查器一样,突出显示可能引发质量或安全问题的源代码问题。该扩展不仅告诉你问题是什么,还提供关于为什么有害以及如何修复它的上下文指导,附带示例。该扩展支持超过500+个Java规则,并包括若干快速修复选项,可自动修复某些质量问题。

启用 -Xlint 以供 VSCode Java 编译使用。

英文:

SonarLint may be what you need.

> SonarLint is an easy-to-use extension that helps you find and fix bugs and security issues as you code. The extension runs in the background and, just like a spell checker, highlights source code issues that pose a quality or security concern. The extension not only tells you what the issue is but also provides in-context guidance on why it's harmful and how to fix it, with examples. The extension supports over 500+ Java rules and includes several Quick Fixes to automatically fix certain quality issues

启用 -Xlint 以供 VSCode Java 编译使用。

答案2

得分: 0

安装Code Runner扩展,然后有两种方法来修改命令

  1. 在设置面板上搜索code-runner.executorMap,点击在 settings.json 中编辑

  2. 在打开的settings.json中进行修改:

    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    

    替换为:

    "java": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
    
  3. 右键单击Run Code (Ctrl+Alt+N),或者点击右上角的三角形按钮选择Run Code

  4. settings.json中添加以下配置:

    "code-runner.customCommand": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
    
  5. 使用以下方法执行脚本:

    Ctrl+Shift+P --> Run Custom Command (Ctrl+Alt+K)

您可以添加以下设置,使Code RunnerTERMINAL而不是OUTPUT面板中输出:

"code-runner.runInTerminal": true,

在正常情况下,我们仍建议您使用Extension Pack for Java来获得更多功能和更好的体验。

英文:

Install the Code Runner extension, and then you have two ways to modify the command

  1. Search code-runner.executorMap on the Seting panel, click Edit in settings.json

    启用 -Xlint 以供 VSCode Java 编译使用。

  2. Modify in the opening settings.json

    		"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    

    for

    	    "java": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
    

    启用 -Xlint 以供 VSCode Java 编译使用。

  3. Right -click Run Code (Ctrl+Alt+N), or the upper right triangle button select Run Code

    启用 -Xlint 以供 VSCode Java 编译使用。


  1. Add the following configuration to settings.json

    	"code-runner.customCommand": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
    
  2. Use the following method to execute the script

    <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> --> Run Custom Command (Ctrl+Alt+K)

    启用 -Xlint 以供 VSCode Java 编译使用。


You can add the following settings to make Code Runner output in the TERMINAL instead of the OUTPUT panel

	&quot;code-runner.runInTerminal&quot;: true,

Under normal circumstances, we still recommend that you use the Extension Pack for Java to obtain more functions and better experiences.

huangapple
  • 本文由 发表于 2023年5月13日 08:57:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76240654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定