英文:
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规则,并包括若干快速修复选项,可自动修复某些质量问题。
英文:
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
答案2
得分: 0
安装Code Runner扩展,然后有两种方法来修改命令
-
在设置面板上搜索
code-runner.executorMap
,点击在 settings.json 中编辑
-
在打开的settings.json中进行修改:
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
替换为:
"java": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
-
右键单击
Run Code (Ctrl+Alt+N)
,或者点击右上角的三角形按钮选择Run Code
-
在settings.json中添加以下配置:
"code-runner.customCommand": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
-
使用以下方法执行脚本:
Ctrl+Shift+P -->
Run Custom Command (Ctrl+Alt+K)
您可以添加以下设置,使Code Runner在TERMINAL而不是OUTPUT面板中输出:
"code-runner.runInTerminal": true,
在正常情况下,我们仍建议您使用Extension Pack for Java来获得更多功能和更好的体验。
英文:
Install the Code Runner extension, and then you have two ways to modify the command
-
Search
code-runner.executorMap
on the Seting panel, clickEdit in settings.json
-
Modify in the opening settings.json
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
for
"java": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
-
Right -click
Run Code (Ctrl+Alt+N)
, or the upper right triangle button selectRun Code
-
Add the following configuration to settings.json
"code-runner.customCommand": "cd $dir && javac -Xlint $fileName && java $fileNameWithoutExt",
-
Use the following method to execute the script
<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd> -->
Run Custom Command (Ctrl+Alt+K)
You can add the following settings to make Code Runner output in the TERMINAL instead of the OUTPUT panel
"code-runner.runInTerminal": true,
Under normal circumstances, we still recommend that you use the Extension Pack for Java to obtain more functions and better experiences.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论