Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more lint rules #1082

Merged
merged 2 commits into from
Feb 20, 2025
Merged

Add more lint rules #1082

merged 2 commits into from
Feb 20, 2025

Conversation

osterman
Copy link
Member

@osterman osterman commented Feb 20, 2025

What

This PR adds and updates several golangci-lint rules to enforce stricter code quality and maintainability standards. Specifically, it:

  • Enhances structured logging validation by configuring loggercheck to support charmbracelet/log, ensuring key-value pairs are formatted correctly.
  • Forbids goto statements by enabling the banned-characters rule.
  • Disallows multi-line errors (\n in error messages) using error-strings to improve error readability.
  • Limits function length (function-length) to prevent overly large and unreadable functions.
  • Restricts file size using file-length-limit to encourage modularity.
  • Enforces cognitive complexity limits (cognitive-complexity) to prevent functions from becoming too complex.
  • Checks cyclomatic complexity (cyclomatic) to encourage simpler, more maintainable logic.
  • Prevents deeply nested if statements (nestif) to improve readability.
  • Standardizes import ordering with gci.
  • Ensures consistent comment formatting (godot).

Why

  • Improved maintainability: Enforcing function size, file size, and complexity limits helps keep code more modular and readable.
  • Better logging practices: By adding loggercheck rules for charmbracelet/log, we ensure structured logging best practices are followed.
  • Stronger error handling: Disallowing multi-line errors improves error message parsing and readability.
  • More readable control structures: By limiting goto and nested if statements, we prevent overly complex code paths.
  • Standardized formatting: Enforcing import ordering and comment formatting ensures consistency across the codebase.
  • Early detection of potential issues: These additional lint rules help catch problematic patterns before they become technical debt.

Summary by CodeRabbit

  • Chores
    • Enhanced internal quality assurance settings to catch issues earlier.
    • Refined code consistency and error detection thresholds, promoting greater stability and a smoother overall experience.
    • Updated linting configuration with new linters and refined settings for improved code quality checks.

@osterman osterman requested a review from a team as a code owner February 20, 2025 18:55
@mergify mergify bot added the triage Needs triage label Feb 20, 2025
Copy link
Contributor

coderabbitai bot commented Feb 20, 2025

📝 Walkthrough

Walkthrough

The pull request updates the .golangci.yml configuration file for golangci-lint. It adds numerous new linters (such as bodyclose, cyclop, err113, gci, gocognit, among others), re-adds previously removed ones like staticcheck and revive, and retains existing ones such as funlen. In addition, the settings for several linters have been revised—adjusting parameters for function length, cognitive complexity, and line length. The issue reporting parameters and test file exclusion rules have also been updated.

Changes

File Change Summary
.golangci.yml Updated linting configuration by adding new linters (bodyclose, cyclop, err113, errcheck, gci, gocognit, godot, gofmt, gosec, loggercheck, nestif, nilerr, rowserrcheck, tparallel, unconvert, unparam, unused, whitespace, wsl), re-adding staticcheck and revive, and retaining funlen. Modified linters settings with new parameters for funlen (max 60 lines, 40 statements, comments ignored), added settings for lll, gci, gofmt, cyclop, gocognit, godot, and revive, and updated issues settings (max-issues-per-linter and max-same-issues set to 0) alongside revised exclusion rules for test files.

Possibly related PRs

Suggested labels

minor

Suggested reviewers

  • aknysh

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4b8c9c2 and 7338441.

📒 Files selected for processing (1)
  • .golangci.yml (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Analyze (go)
  • GitHub Check: Summary
🔇 Additional comments (11)
.golangci.yml (11)

10-37: Comprehensive Linter Additions
A wide array of new linters have been added (e.g. bodyclose, cyclop, err113, errcheck, funlen, gci, gocognit, godot, gofmt, goimports, gosec, loggercheck, nestif, nilerr, revive, rowserrcheck, staticcheck, tparallel, unconvert, unparam, unused, whitespace, wsl). These changes will significantly boost our static analysis and code quality. Please ensure that the team is prepared for the new warnings and that they align with our coding guidelines.


41-43: Enhanced funlen Settings
The funlen configuration now restricts functions to 60 lines and 40 statements, while ignoring comments. This should help keep functions modular and maintainable.


45-49: LLL Configuration
Setting a maximum line length of 120 characters with a tab width of 4 improves readability. These values appear reasonable for our codebase.


51-57: Import Ordering via gci
The import sections are now categorized into standard, default, and a module-specific prefix (your/company/module). Please verify that the prefix correctly reflects your project’s module path.


58-65: Modernizing with gofmt Rewrite Rule
The gofmt settings enforce code simplification and include a rewrite rule to convert interface{} to any. This is great for modern Go code, but please confirm that the project is using Go 1.18 or later to support the any alias.


66-71: Cyclomatic Complexity Check via cyclop
The maximum complexity is now capped at 15, and tests are skipped from this analysis. This should effectively target overly complex functions.


72-75: Cognitive Complexity Configuration with gocognit
A minimum cognitive complexity of 20 has been set. This threshold might flag moderately complex functions, so please ensure that it aligns with your team’s expectations.


76-80: Comment Style Enforcement with godot
Forcing comments to start with a capital letter and end with a period will help maintain a consistent documentation style throughout the codebase.


133-153: Structured Logging with loggercheck
The loggercheck configuration now disables defaults for other logging libraries and enforces that logging keys be inlined constant strings while also defining explicit rules for charmbracelet/log. This change should help prevent logging inconsistencies.


155-158: Nesting Warning via nestif
Setting the minimum complexity of if statements to 4 should help catch overly nested conditions, thereby promoting clearer control flow.


160-167: Stricter Issue Reporting Configuration
The issues section now sets max-issues-per-linter and max-same-issues to 0, and test files are excluded for selected linters (revive and wsl). Verify that setting these limits to 0 aligns with your desired strictness in issue reporting.

@mergify mergify bot removed the triage Needs triage label Feb 20, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
.golangci.yml (1)

72-75: gocognit Settings for Cognitive Complexity
The minimum cognitive complexity threshold is set at 20. Please verify that this threshold is in line with your team’s expectations—especially when compared with the revive rule later set to 25.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7338441 and 52089a4.

📒 Files selected for processing (1)
  • .golangci.yml (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: Build (macos-latest, macos)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Analyze (go)
  • GitHub Check: Summary
🔇 Additional comments (17)
.golangci.yml (17)

10-13: Enabled Linters – HTTP, Complexity, and Error Checks
The addition of the linters bodyclose, cyclop, err113, and errcheck strengthens your linting strategy by covering HTTP response handling, cyclomatic complexity, and proper error checking.


15-17: Enabled Linters – Function Length, Import Order, & Cognitive Complexity
Adding funlen, gci, and gocognit enhances maintainability by enforcing reasonable function size limits, proper import ordering, and detecting overly complex functions. Ensure that these thresholds reflect your project’s needs.


19-20: Enabled Linters – Comment Formatting and Code Style
The inclusion of godot and gofmt enforces consistent comment punctuation and overall code style, which is key for clarity and readability.


22-23: Enabled Linters – Import Formatting & Security Checks
Using goimports for import formatting together with gosec for security scanning helps catch both stylistic and security issues. This should improve overall code quality.


25-25: Enabled Linter – LoggerCheck
The addition of loggercheck supports structured logging best practices, ensuring that log keys remain constant strings. Confirm that your logging practices align with these rules.


27-37: Enabled Linters – Expanded Quality and Style Checks
The extended list of linters from nestif through wsl covers a broad range of code quality aspects—from deep nesting and nil error handling to redundant conversions and whitespace issues. This comprehensive approach should help maintain a high standard, but double-check that none of these rules conflict with your team’s coding style.


41-43: Function Length Settings in funlen
Defining a maximum of 60 lines per function and 40 statements (with comments ignored) is a balanced approach to promote modular and maintainable code.


45-49: Line Length and Tab Width Settings in lll
Setting the maximum line length to 120 characters and a tab width of 4 spaces should help maintain code readability across your project.


51-57: Import Ordering Settings in gci
The configuration for import ordering—with sections for standard, third party, and your module imports—improves code organization. Great work!


58-65: Gofmt Settings for Simplification and Modernization
Enabling simplification (gofmt -s) and rewriting interface{} to any is a smart move to modernize the code base and keep formatting consistent.


66-70: Cyclop Settings for Complexity
A maximum function complexity of 15, with tests excluded, focuses complexity checks on your production code. This is a solid setting to help manage function complexity.


76-80: Godot Settings for Comment Consistency
Enforcing that comments start with a capital letter and end with a period provides a consistent and professional documentation style.


82-117: Revive Linter Comprehensive Rule Set
This block introduces extensive checks via revive, including banned characters, error string formatting, file/function length limits, as well as cognitive and cyclomatic complexity. One key point is the cognitive-complexity threshold set to 25—please confirm that this aligns with your team’s standards, particularly given the slightly different value in the gocognit settings.


133-154: LoggerCheck Settings Detailed Configuration
The customized configuration for loggercheck—disabling checks for alternative logging libraries and enforcing inlined constant strings for log keys—provides robust guidance for maintaining structured logging practices.


155-157: Nestif Settings to Avoid Deeply Nested If Statements
Setting the minimal complexity for if statements to 4 helps catch overly nested code, thus promoting simpler control structures.


160-161: Strict Issue Reporting – Global Settings
Configuring max-issues-per-linter and max-same-issues to 0 enforces a strict reporting regime, which can be very effective in managing technical debt.


166-168: Exclusion Rules for Test Files in Issues Section
Excluding specific linters (funlen, revive, wsl, and gci) for test files may help reduce noise from non-critical issues. Please confirm that this exclusion is intentional and aligns with your overall testing strategy.

@osterman osterman merged commit 2d1bfac into main Feb 20, 2025
46 checks passed
@osterman osterman deleted the add-more-lint-rules branch February 20, 2025 20:07
Copy link

These changes were released in v1.163.0.

@coderabbitai coderabbitai bot mentioned this pull request Feb 20, 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.

1 participant