静态代码检查-Sonar-GO语言扫描规则(二)

1.静态代码检查-Sonar-GO语言扫描规则

1.sonar搭建成功后查看语言扫描规则如图:默认GO语言扫描规则46条。

2.规则总结

1.异味

"=+" should not be used instead of "+="      不应该用"=+"代替"+="

"default" clauses should be first or last         default 应该出现在最开头 或 最结尾

"for" loop increment clauses should modify variables from loop conditions     for“循环增量”子句应该根据循环条件修改变量

"if ... else if" constructs should end with "else" clauses     else if”结构应该以“else”子句结束

"switch case" clauses should not have too many lines     “switch case”子句不应该有太多行

"switch" statements should have "default" clauses         “switch”语句应该有“default”子句

"switch" statements should not be nested                     “switch”语句不应该嵌套

"switch" statements should not have too many "case" clauses          “switch”语句不应该有太多的“case”子句

Boolean literals should not be redundant               布尔文字不应该是多余的

Branches should have sufficient coverage by tests         分支应该有足够的测试覆盖率

Cognitive Complexity of functions should not be too high    功能的认知复杂性不应过高

Collapsible "if" statements should be merged       应该合并可折叠的“if”语句

Control flow statements "if", "for" and "switch" should not be nested too deeply  控制流语句“if”、“for”和“switch”不应该嵌套得太深

Empty statements should be removed   应该删除空语句

Files should not have too many lines of code    文件不应该有太对代码行

Function and method names should comply with a naming convention   函数名和方法名应该符合命名约

Functions and methods should not have too many lines     函数和方法不应该有太多行

Functions should not be empty       函数不应该是空行的

Functions should not have identical implementations   函数不应该有相同的实现

Lines should have sufficient coverage by tests    行应该有足够的测试覆盖率

Lines should not be too long            行不应该太长

Nested blocks of code should not be left empty     嵌套的代码块不应该是空的

Octal values should not be used      不应该使用八进制值

Redundant pairs of parentheses should be removed    应该删除多余的括号对

Skipped unit tests should be either removed or fixed   应该删除或修复跳过的单元测试

Source files should have a sufficient density of comment lines   源文件应该有足够的注释行密度

Source files should not have any duplicated blocks    源文件不应该有任何重复的块

String literals should not be duplicated    字符串文字不应重复

Track lack of copyright and license headers   跟踪缺乏版权和许可证标题

Track uses of "FIXME" tags    跟踪“FIXME”标签的使用

Track uses of "TODO" tags   不应该有 “TODO” 标签

Two branches in a conditional structure should not have exactly the same implementation   条件结构中的两个分支不应该具有完全相同的实现

Unused function parameters should be removed     应该删除未使用的函数参数

2.漏洞

Credentials should not be hard-coded      凭证不应该硬编码

IP addresses should not be hardcoded     IP地址不应该硬编码

3.bug

All branches in a conditional structure should not have exactly the same implementation  条件结构中的所有分支不应该具有完全相同的实现

Collection sizes and array length comparisons should make sense    集合大小和数组长度比较应该有意义

Failed unit tests should be fixed   失败的单元测试应该被修复

Identical expressions should not be used on both sides of a binary operator   二进制运算符的两边不应该使用相同的表达式

Jump statements should not be followed by dead code  跳转语句后面不应该跟着死代码

Loops with at most one iteration should be refactored   应该重构最多一次迭代的循环

Related "if/else if" statements should not have the same condition  相关的“if/else if”语句不应该具有相同的条件

Unary prefix operators should not be repeated    不应重复使用一元前缀运算符

Useless "if(true) {...}" and "if(false){...}" blocks should be removed   无用的“如果(真正的){…}”和“如果(false){…}”块应该被删除

Variables should not be self-assigned   变量不应该自行分配

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
在上一篇文章中,我们介绍了如何安装 SonarQube 和 SonarScanner,并扫描了一个 C# 项目。本文将介绍如何扫描 Java 项目。 ## 准备工作 首先,我们需要安装 Java 环境。可以从官方网站上下载并安装 JDK。 另外,我们还需要安装 Maven。可以从官方网站上下载并安装 Maven。 ## 配置 SonarQube 与上一篇文章相同,我们需要在 SonarQube 中添加 Java 插件。在 SonarQube 的插件页面中搜索 Java 并安装。 ## 配置项目 在扫描 Java 项目之前,我们需要在项目中添加一个 SonarQube 插件。在项目的 pom.xml 文件中添加以下内容: ```xml <build> <plugins> <plugin> <groupId>org.sonarsource.scanner.maven</groupId> <artifactId>sonar-maven-plugin</artifactId> <version>3.6.0.1398</version> </plugin> </plugins> </build> ``` ## 执行扫描 在项目的根目录下,执行以下命令: ``` mvn sonar:sonar \ -Dsonar.projectKey=<项目键> \ -Dsonar.host.url=http://localhost:9000 \ -Dsonar.login=<访问令牌> ``` 其中,`<项目键>` 是在 SonarQube 中创建项目时指定的项目键;`http://localhost:9000` 是 SonarQube 的地址;`<访问令牌>` 是在 SonarQube 中创建用户并生成的访问令牌。 执行完毕后,访问 SonarQube 网站,可以看到 Java 项目的扫描结果。 ## 结论 通过上述步骤,我们可以轻松地扫描 Java 项目,并通过 SonarQube 分析代码质量。如果想了解更多关于 SonarQube 的用法,可以查看官方文档。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值