Skip to content

Conversation

@chelout
Copy link
Contributor

@chelout chelout commented Oct 27, 2025

Summary

Adds support for detecting SELECT * usage in const and var declarations at package and function levels.

Problem

Previously, the analyzer only detected SELECT * in:

  • Short variable declarations: query := "SELECT * FROM users"
  • Function call arguments: db.Query("SELECT * FROM users")

But missed a common pattern of storing SQL queries in constants:

const UserQuery = "SELECT * FROM users"  // ❌ Not detected
var OrderQuery = "SELECT * FROM orders"  // ❌ Not detected

Solution

Core Changes

  • Added checkGenDecl() function to process *ast.GenDecl nodes (const/var declarations)
  • Integrated into both entry points: run() and RunWithConfig()
  • Now detects:
    • ✅ Package-level constants: const Query = "SELECT * FROM users"
    • ✅ Package-level variables: var Query = "SELECT * FROM users"
    • ✅ Declaration blocks: const (...) and var (...)
    • ✅ Local constants inside functions

Documentation Updates

  • Updated README.md with new feature description
  • Added examples for constants and variables

Examples

❌ Will be detected:

const QueryUsers = "SELECT * FROM users"
var DynamicQuery = "SELECT * FROM logs"

const (
    GetAllUsers = "SELECT * FROM users"
    GetOrders   = "SELECT * FROM orders"
)

func example() {
    const localQuery = "SELECT * FROM categories"
}

✅ Good practices:

const GoodQuery = "SELECT id, name, email FROM users"
const CountQuery = "SELECT COUNT(*) FROM users"
const SchemaQuery = "SELECT * FROM information_schema.tables"

Technical Details

AST Analysis confirms no duplication:

  • checkAssignStmt handles *ast.AssignStmt: short declarations (:=) and assignments (=)
  • checkGenDecl (new) handles *ast.GenDecl: const and var declarations
  • Functions are complementary with no overlap

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

@MirrexOne Александр, привет!
Надеялся, что правки включат в golangci-lint следующей версии, но уже не судьба... Было бы классно сделать это в следующей версии :)

@MirrexOne
Copy link
Owner

Привет)
Благодарю за проявленный интерес и PR с улучшениями.
Со своей стороны возьму в приоритет то, чтобы изменения появились в следующей версии.

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Спасибо большое, буду ждать :)

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Ну и в догонку, готов внести необходимые изменения в свой PR или объяснить какие-то моменты

@MirrexOne
Copy link
Owner

Насколько я поял, в нынешней актуальной версии, package-level константы и переменные с паттерном * - не детектились, верно ? Или возникали случаи false-positive \ false-negative ?

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Да, все так. Такие константы и переменные не проверялись

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Про false-positive \ false-negative речи не идет

@MirrexOne
Copy link
Owner

MirrexOne commented Nov 3, 2025

Понял. Спасибо большое, что заметили. Это, действительно, видится важным изменением.
Изменения посмотрел - всё гуд.

@MirrexOne MirrexOne merged commit 17a9685 into MirrexOne:main Nov 3, 2025
9 checks passed
@chelout chelout deleted the feature-general-declarations branch November 3, 2025 22:29
@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Спасибо! 😃

@chelout
Copy link
Contributor Author

chelout commented Nov 3, 2025

Будем ждать в следующий версии golangci-lint 😃

@chelout
Copy link
Contributor Author

chelout commented Nov 4, 2025

Извини за назойливость, если все хорошо, может сделаешь новый релиз, чтобы он попал в golangci-lint? 😊

@MirrexOne
Copy link
Owner

Ничего страшного, всё понимаю и сейчас буду как раз таки создавать релиз. Как закончю(это будет сегодня) - уведомлю)

@MirrexOne
Copy link
Owner

@chelout Новый релиз готов. Подскажи, пожалуйста, могу что-то ещё сделать ? Новый релиз, насколько знаю, dependabot в golangci-lint подтянет ?

@chelout
Copy link
Contributor Author

chelout commented Nov 5, 2025

Спасибо еще раз. Да, полагаю автоматом подтянется в следующий релиз

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants