0
点赞
收藏
分享

微信扫一扫

ClickHouse快速安装教程(MacOS)

目录

一、SonarQube质量报告

二、SonarQube扫描常见问题和修复方法

三、SonarQube质量配置


最近小编在使用SonarQube工具进行代码扫描,检查代码异味,系统漏洞等,实际过程中也遇到了不少问题,这篇文章主要列举我遇到的常见问题和解决方法。

如何使用SonarQube进行安全扫描,可以查看往期文章:SonarQube安全扫描_sonarqube 安全扫描-CSDN博客

一、SonarQube质量报告

进入到SonarQube首页,可以看到项目报告的全部信息,如下图所示:

 点击项目名称,可查看报告详情,New Code(新增代码)Overall Code(全量代码)


二、SonarQube扫描常见问题和修复方法

1.(不应重复字符串文字)String literals should not be duplicated.

 上述代码可修改为:

const errmsg = "check request fields required failed, %w"
err, errResp := CheckRequestFieldsRequired(req, reflect.TypeOf(*req))
	if err != nil {
		return fmt.Errorf(errmsg, err), errResp

	}

2.(认知功能的复杂度不应过高)Cognitive Complexity of functions should not be too high.

3.(函数不能为空)Functions should not be empty.

4.(函数不应该有太多的参数)Functions should not have too many parameters.

 上述代码可修改为:

type AssembleHPCJobRequestParams struct {
    Context            context.Context
    Logger             *zap.SugaredLogger
    Job                *models.Job
    AppImage           string
    EnvVars            map[string]string
    SchedulerSubmitFlags map[string]string
    NoTransfer         bool
    LocalImage         bool
    CoresPerNode       int
}


func AssembleHPCJobRequest(params AssembleHPCJobRequestParams) hpc.SystemPostRequest {
    // 函数的实现逻辑...
}

//在调用这个函数的时候
params := AssembleHPCJobRequestParams{
    Context:            ctx,
    Logger:             logger,
    Job:                job,
    AppImage:           appImage,
    EnvVars:            envVars,
    SchedulerSubmitFlags: schedulerSubmitFlags,
    NoTransfer:         noTransfer,
    LocalImage:         localImage,
    CoresPerNode:       coresPerNode,
}

request := AssembleHPCJobRequest(params)

5.(局部变量和函数参数名称应遵守命名约定)Local variable and function parameter names should comply with a naming convention.

 上述命名可修改为:可以将user_combo更名为userCombo,将turn_info_each更名为turnInfoEach


三、SonarQube质量配置

质量规则

go:Sonar way Bug:6 坏味道:17

规则类型违规级别中文

Variables should not be self-assigned

Bug主要变量不应自赋值

Identical expressions should not be used on both sides of a binary operator

Bug主要不应在二元运算符的两侧使用相同的表达式

All code should be reachable

Bug主要所有代码都应该是可访问的

Related "if/else if" statements should not have

the same condition

Bug主要相关的“if/elseif'语句不应具有相同的条件

"=+" should not be used instead of "+="

Bug主要不能用“=+”代替“+=”

All branches in a conditional structure should not have exactly the same implementation

Bug主要条件结构中的所有分支都不应有完全相同的实现

Cognitive Complexity of functions should not be too high

坏味道

严重认知功能的复杂度不应过高

String literals should not be duplicated

坏味道严重不应重复字符串文字

Functions should not be empty

坏味道严重函数不能为空

Track uses of "FIXME" tags

坏味道主要跟踪"FIXME"标签的使用情况

Two branches in a conditional structure should

not have exactly the same implementation

坏味道主要条件结构中的两个分支不应该有完全相同的实现

Redundant pairs of parentheses should be

removed

坏味道主要应删除多余的圆括号对

Functions should not have identical

implementations

坏味道主要函数不应该有相同的实现

Track parsing failures

坏味道主要跟踪解析失败

"switch" statements should not have too many

"case" clauses

坏味道主要"switch"语句不应该有太多的"case"从句

Functions should not have too many parameters

坏味道主要函数不应该有太多的参数

Nested blocks of code should not be left empty

坏味道主要嵌套的代码块不应为空

Multi-line comments should not be empty

坏味道次要多行注释不应为空

Boolean checks should not be inverted

坏味道次要布尔值检查不应倒置

Boolean literals should not be redundant

坏味道次要布尔字面值不应是多余的

Function and method names should comply with a naming convention

坏味道次要函数和方法的名称应符合
命名约定

Local variable and function parameter names

should comply with a naming convention

坏味道次要局部变量和函数参数名称应遵守命名约定

Track uses of "TODO" tags

坏味道提示跟踪“TODO”标签的使用

经过上述分享,应该对SonarQube工具进行安全扫描和解决代码异味有了一定的了解,请继续关注小编~带来更多的分享哦~

举报

相关推荐

0 条评论