Skip to content

Make checkswitch handle regular switches #47669

Make checkswitch handle regular switches

Make checkswitch handle regular switches #47669

Triggered via pull request April 15, 2024 07:50
@AlekSiAlekSi
labeled #4162
Status Success
Total duration 39s
Artifacts

conform-pr.yml

on: pull_request_target
Conform PR
31s
Conform PR
Fit to window
Zoom out
Zoom in

Annotations

1 error
Conform PR
New test cases handled to ensure switch statements are accounted for\r\n\r\n# Description\r\n\r\n**PR Description:**\r\n\r\nThis PR introduces changes to the `checkSwitch` function in `checkswitch.go` to improve the handling of switch statements in Go code. \r\n\r\nThe changes include:\r\n\r\n1. Handling of switch statements (`*ast.SwitchStmt`) in Go code.\r\n2. Iteration over each case in the switch statement.\r\n3. Special handling for cases with no expressions (i.e., default cases).\r\n4. Special handling for cases with more than one expression.\r\n\r\nThe 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.\r\n\r\nThe 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.\r\n\r\nReviewers 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.\r\n\r\nCloses #4082\r\n\r\n## Open Questions\r\n\r\nQuestion 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. \r\n\r\n## Why does token.STRING work here and why that is added? (this is my understanding, please correct if incorrect)\r\n\r\nIn 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).\r\n\r\nWhen 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.\r\n\r\nWhen 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.\r\n\r\n## Readiness checklist\r\n\r\n\u003c!-- Please check all items in this checklist that were done. --\u003e\r\n\r\n- [x] I added/updated unit tests (and they pass).\r\n- [x] I added/updated comments and checked rendering.\r\n- [x] I made spot refactorings.\r\n- [x] I ran `task all`, and it passed.\r\n- [x] I ensured that PR title is good enough for the changelog.\r\n- [x] (for maintainers only) I set Reviewers ([`@FerretDB/core`](https://github.com/orgs/FerretDB/teams/core)), Milestone (`Next`), Labels, Project and project's Sprint fields.\r\n- [x] I marked all done items in this checklist.\r\n",