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 a linter to catch fmt.Print* #4782

Merged
merged 1 commit into from
Dec 18, 2024

Conversation

wdbaruni
Copy link
Member

@wdbaruni wdbaruni commented Dec 18, 2024

Summary by CodeRabbit

  • New Features

    • Introduced a new linter for improved logging practices.
  • Bug Fixes

    • Removed unnecessary fmt package usage to streamline output.

Copy link
Contributor

coderabbitai bot commented Dec 18, 2024

Walkthrough

The pull request introduces a configuration change in the .golangci.yml file to add the forbidigo linter, which restricts the use of fmt.Print* functions. Correspondingly, the main.go file has been updated to remove the fmt package import and a fmt.Println() call, aligning with the new linting rules that discourage direct printing methods.

Changes

File Change Summary
.golangci.yml Added forbidigo linter configuration to forbid fmt.Print* functions
main.go Removed fmt package import and fmt.Println() method call

Poem

🐰 A Linter's Tale of Printing Woe

No more fmt.Println() to and fro,
Strict rules now guide our coding flow
A rabbit's wisdom, clean and bright
Logging reimagined with delight!

🚫🖨️


🪧 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 to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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

linear bot commented Dec 18, 2024

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)

10-12: Add documentation for the linting rule

While the pattern correctly catches all fmt.Print* variants, it would be helpful to add a description explaining why these functions are forbidden and what alternatives should be used instead.

 forbidigo:
   forbid:
-    - 'fmt\.Print.*'  # This will catch Printf, Println, Print
+    - p: 'fmt\.Print.*'
+      msg: "Please use zerolog instead of fmt.Print* for logging. See logging.md for guidelines."
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 71a58d9 and 70b455e.

⛔ Files ignored due to path filters (1)
  • pkg/config/types/gen/generate.go is excluded by !**/gen/**
📒 Files selected for processing (2)
  • .golangci.yml (2 hunks)
  • main.go (0 hunks)
💤 Files with no reviewable changes (1)
  • main.go
🔇 Additional comments (2)
.golangci.yml (2)

10-12: Verify impact on existing codebase

The linter configuration looks good, but let's assess its impact on the existing codebase.

Run this script to find existing fmt.Print* usage that will be affected:

Also applies to: 76-76

✅ Verification successful

Let me check the specific usage context in these files to ensure they are only in test files.


Linter rule impact is limited to test files only

The fmt.Print* usage appears exclusively in test files and test utilities:

  • All occurrences are in files with _test.go suffix or in test integration utilities
  • Used primarily for debugging and test output purposes
  • No production code is affected
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all fmt.Print* usage in the codebase to assess the impact

# Search for fmt.Print* usage
echo "Files containing fmt.Print* calls:"
rg -l 'fmt\.(Print|Printf|Println)\b' --type go

# Count total occurrences
echo -e "\nTotal occurrences by type:"
rg -c 'fmt\.(Print|Printf|Println)\b' --type go | grep -v ':0$'

Length of output: 635


Script:

#!/bin/bash
# Show the context of fmt.Print* usage in each file
echo "=== Checking usage context ==="
rg 'fmt\.(Print|Printf|Println)\b' --type go -C 2

Length of output: 3071


76-76: LGTM! Verify linter compatibility

The forbidigo linter is correctly enabled and properly positioned in alphabetical order.

Let's verify if there are any potential conflicts with other enabled linters:

✅ Verification successful

Let me try a different approach to verify the linter compatibility.


Linter compatibility verified - no conflicts detected

The forbidigo linter is properly configured with a specific rule to forbid fmt.Print* functions. The configuration is well-integrated with other linters, and there are no overlapping or conflicting rules with other enabled linters in the configuration. The linter's position in the enabled list and its configuration are both correct.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any known conflicts or issues between forbidigo and other enabled linters

# Search for any reported issues or conflicts
gh api graphql -f query='
{
  search(query: "repo:golangci/golangci-lint forbidigo conflict", type: ISSUE, first: 5) {
    nodes {
      ... on Issue {
        title
        url
        state
      }
    }
  }
}'

Length of output: 261


Script:

#!/bin/bash
# Check the current configuration and other enabled linters
cat .golangci.yml

# Also check if there are any specific configurations for forbidigo
rg -A 5 "forbidigo" .golangci.yml

Length of output: 4749

@wdbaruni wdbaruni merged commit 7cf0f35 into main Dec 18, 2024
14 checks passed
@wdbaruni wdbaruni deleted the eng-131-add-a-linter-to-catch-fmtprint branch December 18, 2024 08:28
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