gtime.Time对象通过Format方法来实现自定义格式的时间日期转换,该方法与标准库time.Time的Format方法冲突。在gtime.Time对象中,通过Layout方法实现标准库time.Time的Format格式,例如:t.Layout(2006-01-02 15:04:05)。

gtime.Time是GoFrame框架的时间类型,它的Format方法的形参与标准库Format方法的形参完全不同。
很多人会使用错误,而且使用错误不会报错;导致难以发现。
本代码只是Demo阶段,未测试,不能保证识别的准确和完整性
文末有示例代码,用于识别测试
本代码参考:https://disaev.me/p/writing-useful-go-analysis-linter/

package analyzerimport ("flag""go/ast""golang.org/x/tools/go/analysis""strings"
)var Analyzer = &analysis.Analyzer{Name:  "gtimeFormatLinter",Doc:   "gtime.Time is the time type of the Go Frame framework. The formal parameters of its Format method are completely different from those of the Format method in the standard library.",Flags: flag.FlagSet{},Run:   run,
}func callMultiplexing(call *ast.CallExpr, pass *analysis.Pass, formatSymbols []string) {if call.Args == nil || len(call.Args) == 0 {return}for _, arg := range call.Args {if arg == nil {continue}bas, ok := arg.(*ast.BasicLit)if !ok {continue}var existence boolfor _, formatSymbol := range formatSymbols {if strings.Contains(bas.Value, formatSymbol) {existence = true}}if existence {pass.Reportf(bas.Pos(), "Incorrect formal parameters of the Format method of gtime.Time")}}
}
func run(pass *analysis.Pass) (interface{}, error) {var formatSymbols = []string{"2006", "01", "02", "15", "04", "05"}inspect := func(node ast.Node) bool {fun, ok := node.(*ast.FuncDecl)if !ok {return true}if fun.Body == nil || fun.Body.List == nil || len(fun.Body.List) == 0 {return true}for _, stmt := range fun.Body.List {if stmt == nil {continue}expr, ok := stmt.(*ast.ExprStmt)if !ok {continue}if expr.X == nil {continue}call, ok := expr.X.(*ast.CallExpr)if !ok {continue}if call.Fun == nil {continue}sel, ok := call.Fun.(*ast.SelectorExpr)if !ok {continue}if sel.Sel == nil || sel.Sel.Name != "Format" {continue}if sel.X == nil {continue}switch sel.X.(type) {case *ast.Ident:id := sel.X.(*ast.Ident)if id.Obj == nil {continue}if id.Obj.Decl == nil {continue}obj, ok := id.Obj.Decl.(*ast.ValueSpec)if !ok {continue}if obj.Type == nil {continue}sel2, ok := obj.Type.(*ast.SelectorExpr)if !ok {continue}if sel2.X == nil {continue}id2, ok := sel2.X.(*ast.Ident)if !ok {continue}if id2.Name != "gtime" {continue}if sel2.Sel == nil {continue}if sel2.Sel.Name != "Time" {continue}callMultiplexing(call, pass, formatSymbols)case *ast.CallExpr:call2, ok := sel.X.(*ast.CallExpr)if !ok {continue}if call2.Fun == nil {continue}sel2, ok := call2.Fun.(*ast.SelectorExpr)if !ok {continue}if sel2.X == nil {continue}id, ok := sel2.X.(*ast.Ident)if !ok {continue}if id.Name != "gtime" {continue}callMultiplexing(call, pass, formatSymbols)}}return true}for _, file := range pass.Files {ast.Inspect(file, inspect)}return nil, nil
}
package mainimport ("github.com/gogf/gf/os/gtime""time"
)func A() {var t gtime.Timet.Format("2006-01-02 15:04:05")gtime.New().Format("2006-01-02 15:04:05")gtime.NewFromStr("2006-01-02 15:04:05").Format("2006-01-02 15:04:05")t.Format("%Y-m-d")gtime.New().Format("%Y-m-d")var sy time.Timesy.Format("2006-01-02 15:04:05")time.Now().Format("2006-01-02 15:04:05")
}

golangci-lint 自定义linter核心代码 用于检查错误使用gtime.Time.Format() 例如gtime.Time.Format(“2006-01-02 15:04:05“)相关推荐

  1. cppcheck 自定义规则_cppcheck代码静态检查工具及相关工具插件用法介绍

    摘要:介绍代码缺陷静态检查工具(static code analyzer)cppcheck,以及其vs.qtcreator.git.jenkins插件及用法. Cppcheck着重于检测未定义的行为和 ...

  2. C# 双色球核心代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. Go语言如何自定义 linter(静态检查工具)

    前言 哈喽,大家好,我是asong: 通常我们在业务项目中会借助使用静态代码检查工具来保证代码质量,通过静态代码检查工具我们可以提前发现一些问题,比如变量未定义.类型不匹配.变量作用域问题.数组下标越 ...

  4. Go 语言如何自定义 linter(静态检查工具)

    前言 哈喽,大家好,我是asong: 通常我们在业务项目中会借助使用静态代码检查工具来保证代码质量,通过静态代码检查工具我们可以提前发现一些问题,比如变量未定义.类型不匹配.变量作用域问题.数组下标越 ...

  5. Go语言如何自定义linter(静态检查工具)

    前言 哈喽,大家好,我是asong: 通常我们在业务项目中会借助使用静态代码检查工具来保证代码质量,通过静态代码检查工具我们可以提前发现一些问题,比如变量未定义.类型不匹配.变量作用域问题.数组下标越 ...

  6. antd自定义分页器_从零开始实现类 antd 分页器(二):分页核心代码

    本文是使用 Typescript 开发类 antd 分页器,并发布 npm 的第二篇,因为最近在业务中使用了 antd 中的 Pagination 开发了相关业务,而自己又对组件的封装由很有兴趣,所以 ...

  7. python重复三角形代码_用于检查Python中两个三角形的一致性的程序

    在本教程中,我们将检查两个三角形的一致性.我们将检查SSS,SAS和AAA.基于这些标准证明了三角形的相似性. 我们必须根据定理检查不同的条件.在下面的代码中检查它们. 示例def side_side ...

  8. Gopher一定要会的代码自动化检查

    本文讲解如何通过 golangci-lint 和 pre-commit 两大框架,利用 git hooks 实现 Go 语言 git commit 的代码自动化审查. 静态代码检查 静态代码检查是一个 ...

  9. swift/dart代码规范检查工具介绍

    swift/dart代码规范检查工具介绍 简介: 本篇主要介绍swift和dart代码规范检查工具,以及他们的工作原理,操作过程,代码规范规则. 1 swift代码检查工具-swiftlint 1.1 ...

最新文章

  1. jpg怎么合成一份_哪些超实用的有机化学知识点---之有机合成工具包
  2. php 数组合并_PHP数组常用函数分类整理
  3. sql stuff 函数_SQL STUFF函数概述
  4. Android Studio报错:Plugin Kotlin was not installed: Cannot download
  5. 【转】C#通过WMI设置NTFS目录共享和目录安全
  6. Win10 安装 .NetFramework 4.7 (SourceTree)
  7. php 登陆代码,php登陆代码_php实现简单用户登录功能程序代码
  8. 历史要横着读,故事要竖着看!嘻哈~
  9. 向日葵 远程开机 linux,教你使用向日葵开机棒轻松实现远程开机
  10. sox处理mp3_SOX 音频处理
  11. android脚本精灵开发,触动精灵脚本开发示例代码
  12. 目标检测, 实例分割, 图像分类, panoptic segmentation文献
  13. Setup Factory导入注册表时丢失部分语句
  14. 烂泥:linux文件同步之rsync学习(一)
  15. (iptables)火墙策略读取的先后顺序 + 引入数据包状态
  16. 一文快速搞懂对95%置信区间的理解
  17. 借项通知单 贷项通知单
  18. ABeam×StartUp | ABeam旗下艾宾信息技术开发(大连)与大连金勺科技展开合作交流
  19. 鼠标在滑块上滚轮控制_200块钱的无线蓝牙鼠标好在哪里?洛斐薯片:除了颜值,也很实用...
  20. 华为云计算HCIA题目1

热门文章

  1. H5学习之旅-H5的基本标签(2)
  2. rtx2060相当于gtx多少 rtx2060属于什么档次的显卡 rtx2060显卡怎么样
  3. java如何虚拟ip_Linux建立虚拟ip的方法
  4. 志强系列的服务器能吃鸡吗,英特尔 xeon e5-2660能玩绝地求生吗
  5. Matlab从细胞型转换为矩阵From cell to matrix
  6. 从易经看程序员的职业人生:乾卦--天行健,君子以自强不息
  7. 图像的几何变换—平移、旋转、镜像、缩放、剪切(原理+调用函数+像素操作)
  8. 图片优化对SEO有着重要作用
  9. 查查自己五行属什么命
  10. dva如何去掉hash