我SonarCloud在这段代码中遇到错误:
foreach (var item in itemList)
{
if (string.IsNullOrEmpty(item.Name))
{
throw new BadRequestException("Item name is null or missing...");
}
if (someOtherList.Any(x => x.Name == item.Name))
{
throw new NotAcceptedException("Item name already exist in Db.");
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当我有时如何将此代码转换为LINQ使用(如声纳建议) ?Selectexceptions
我有一个来自数据库的条目,格式如下。我首先使用 replaceAll 而不是 replace 函数,但 SonarCloud 显示错误并要求从 replaceAll 更改为 replace。但是,替换并不会替换所有的反斜杠,事实上它不会更改文本中的任何内容。
用java代码从数据库中输入:
""{\"id\":\"5947223f-9c50-425f-950b-e126c94b3adf\",\"name\":\"shruti\"}
Run Code Online (Sandbox Code Playgroud)
使用 replaceAll 我编写的代码如下,工作正常:
abcString.replaceAll("\\\\", "").replaceFirst("\"","");
Run Code Online (Sandbox Code Playgroud)
为了让 SonarCloud 开心,我尝试将 replaceAll 更改为 replace ,这反过来告诉是删除 replaceFirst 方法,否则它会从数据库输出中删除双引号。
具有替换功能的代码:
abcString.replace("\\\\", "")
Run Code Online (Sandbox Code Playgroud)
我尝试的第二件事给出了错误:
Unexpected character ('\' (code 92)): was expecting double-quote to start field name
! at [Source: (String)"{\"id\":\"5947223f-9c50-425f-950b-e126c94b3adf\",\"name\":\"shruti\"}
Run Code Online (Sandbox Code Playgroud) 我用 Java 管理一个开源项目,在我的代码中有大约 20 个地方我使用以下模式记录异常(slf4j 版本 1.7.30)
private static final Logger logger = LoggerFactory.getLogger();
...
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException ex) {
logger.error("Socket exception when retrieving interfaces: {}", ex);
}
Run Code Online (Sandbox Code Playgroud)
或类似
try {
// stuff
} catch (IOException ioe) {
logger.error("Server error: {}", ioe);
}
Run Code Online (Sandbox Code Playgroud)
从今天开始,SonarCloud自动代码质量审查已开始使用规则标记这些java:S2275(Printf 样式的格式字符串不应导致运行时出现意外行为),并带有特定消息“参数不足”。
编辑:值得注意的是,当 anException是最后一个参数时,这似乎总是发生。下面的模式确实没有标志:
try {
// Server connection code
} catch (IOException e) {
logger.error("Server Connection error: {}", e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
我正在寻找为我们的一个 JavaScript 项目配置 sonarcloud 分析的步骤,并使用 Azure DevOps 作为构建平台。
以下链接为我们提供了一些开始信息。
我index.ts在 NPM 库中有一个顶级文件,其中仅包含导出语句。
IE:
export * from "./foo"
export * from "./bar"
Run Code Online (Sandbox Code Playgroud)
SonarCloud 将这些线路显示为未覆盖,因为预计这些线路不应进行测试。我知道我们可以忍受缺少报道,但这在某种程度上很烦人。
我知道我也可以忽略该文件,但随后我需要对具有类似目的的每个文件执行相同的操作,将组件分组并导出到库内。
我可以使用任何最佳实践或配置来克服这个问题吗?
我们有一个设置,我们将 SonarQube 与 Ant 一起使用并连接到 SonarCloud ( https://sonarcloud.io )。此设置始终在本地开发人员机器和 bitbucket 管道中工作。两天以来,我们在本地和 bitbucket 管道上一直低于错误。以下是堆栈跟踪:
Project.sonar:
[sonar:sonar] Apache Ant(TM) version 1.10.5 compiled on July 10 2018
[sonar:sonar] SonarQube Ant Task version: 2.5
[sonar:sonar] Loaded from: file:<redacted_path>/sonarqube-ant-task-2.5.jar
[sonar:sonar] User cache: /Users/<user>/.sonar/cache
BUILD FAILED
<redacted_path>/build-sonar.xml:120: java.lang.IllegalStateException: not started
at org.sonarsource.scanner.api.EmbeddedScanner.checkLauncherExists(EmbeddedScanner.java:244)
at org.sonarsource.scanner.api.EmbeddedScanner.stop(EmbeddedScanner.java:164)
at org.sonarsource.scanner.ant.SonarQubeTask.launchAnalysis(SonarQubeTask.java:101)
at org.sonarsource.scanner.ant.SonarQubeTask.execute(SonarQubeTask.java:81)
at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
at org.apache.tools.ant.Task.perform(Task.java:350)
at org.apache.tools.ant.Target.execute(Target.java:449)
at org.apache.tools.ant.Target.performTasks(Target.java:470)
at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1388)
at org.apache.tools.ant.Project.executeTarget(Project.java:1361)
at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
at org.apache.tools.ant.Project.executeTargets(Project.java:1251)
at org.apache.tools.ant.Main.runBuild(Main.java:834)
at …Run Code Online (Sandbox Code Playgroud) sonarcloud ×6
sonarqube ×3
java ×2
.net ×1
azure-devops ×1
c# ×1
javascript ×1
jestjs ×1
slf4j ×1
string ×1
ts-jest ×1
typescript ×1