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

Make checkswitch handle regular switches #4162

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sbshah97
Copy link
Contributor

@sbshah97 sbshah97 commented Mar 9, 2024

  1. checkswitch.go: Code added to handle switch statements
  2. testdata.go: New test cases handled to ensure switch statements are accounted for

Description

PR Description:

This PR introduces changes to the checkSwitch function in checkswitch.go to improve the handling of switch statements in Go code.

The changes include:

  1. Handling of switch statements (*ast.SwitchStmt) in Go code.
  2. Iteration over each case in the switch statement.
  3. Special handling for cases with no expressions (i.e., default cases).
  4. Special handling for cases with more than one expression.

The reasons for these changes are to ensure that the checkSwitch function correctly identifies and reports cases in switch statements that are out of order. This is important for maintaining code readability and consistency.

The decision to handle cases with no expressions and cases with more than one expression separately was made because these cases require different logic. For cases with no expressions, there is no need to check the order, so these cases are skipped. For cases with more than one expression, each expression needs to be checked individually, so a separate loop is used for this.

Reviewers should know that these changes improve the robustness of the checkSwitch function and ensure that it can handle a wider variety of switch statements. The changes have been thoroughly tested to ensure that they work as expected. However, reviewers are encouraged to scrutinize the changes closely, especially the handling of cases with more than one expression, as this is a complex part of the code.

Closes #4082

Open Questions

Question to @AlekSi and other reviewers: I don't really understand how the test cases work. I have added them as per my understanding, however if I comment my implementation and run the tests they still pass. Maybe I am missing something but your guidance will help here.

Why does token.STRING work here and why that is added? (this is my understanding, please correct if incorrect)

In Go, token.STRING is a constant that represents the string type in Go's token package. It's used when parsing Go source code into an Abstract Syntax Tree (AST).

When you're dealing with a *ast.SwitchStmt in Go, the cases of the switch can be any expression. In your context, you're dealing with a switch statement that switches over bson.tag. The bson.tag is a string that represents the type of the BSON element.

When the Go source code is parsed into an AST, this bson.tag string becomes a *ast.BasicLit with a Kind of token.STRING. That's why the code checks if the firstTypeCase is a *ast.BasicLit with a Kind of token.STRING. If it is, it means that this case of the switch statement is switching over a bson.tag.

Readiness checklist

  • I added/updated unit tests (and they pass).
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Milestone (Next), Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

1. checkswitch.go: Code added to handle switch statements
2. testdata.go: New test cases handled to ensure switch statements are accounted for
@sbshah97 sbshah97 requested a review from AlekSi as a code owner March 9, 2024 23:08
Copy link

codecov bot commented Mar 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.89%. Comparing base (419f9a0) to head (d3a8f91).
Report is 13 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4162      +/-   ##
==========================================
- Coverage   75.01%   73.89%   -1.13%     
==========================================
  Files         339      339              
  Lines       22827    22827              
==========================================
- Hits        17124    16868     -256     
- Misses       4410     4677     +267     
+ Partials     1293     1282      -11     

see 28 files with indirect coverage changes

Flag Coverage Δ
filter-false ?
filter-true 67.36% <ø> (-1.50%) ⬇️
hana-1 ?
integration 67.36% <ø> (-1.54%) ⬇️
mongodb-1 5.13% <ø> (ø)
mysql-1 ?
mysql-2 ?
mysql-3 ?
postgresql-1 46.62% <ø> (-0.07%) ⬇️
postgresql-2 49.57% <ø> (-0.11%) ⬇️
postgresql-3 49.89% <ø> (-0.13%) ⬇️
sqlite-1 45.69% <ø> (-0.11%) ⬇️
sqlite-2 48.75% <ø> (-0.09%) ⬇️
sqlite-3 49.03% <ø> (-0.14%) ⬇️
unit 32.55% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@AlekSi AlekSi added the code/chore Code maintenance improvements label Mar 11, 2024
@AlekSi AlekSi added this to the Next milestone Mar 11, 2024
@AlekSi AlekSi changed the title Enhanced Handling of Switch Statements in Go Static Analysis Tool Make checkswitch handle regular switches Mar 11, 2024
@AlekSi AlekSi enabled auto-merge (squash) March 11, 2024 06:05
Copy link
Member

@AlekSi AlekSi left a comment

Choose a reason for hiding this comment

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

Please check our contributing documentation about code comments.


// Iterate over each case in the switch statement.
for _, el := range n.Body.List {
// If a case has no expressions (i.e., it's a default case), skip to the next case.
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually have default cases? I think we have linter enabled to check that switches are exhaustive. Maybe that tool should double-check that too.

@AlekSi
Copy link
Member

AlekSi commented Mar 11, 2024

The bson.tag is a string

But it is not a string:

type tag byte

Copy link
Member

Choose a reason for hiding this comment

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

Those tests rely on "magic" want comments: https://pkg.go.dev/golang.org/x/tools/go/analysis#hdr-Testing_an_Analyzer There are no such comments, so that test doesn't check anything.

@AlekSi AlekSi removed this from the Next milestone Mar 19, 2024
@sbshah97
Copy link
Contributor Author

Hi thanks for your review. Will look at this again this week.

@AlekSi AlekSi added the community Issues and PRs assigned to community members label Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/chore Code maintenance improvements community Issues and PRs assigned to community members
Projects
Status: In progress
Development

Successfully merging this pull request may close these issues.

checkswitch should handle regular switches
2 participants