sarif-rs icon indicating copy to clipboard operation
sarif-rs copied to clipboard

feat: collect clippy's results children spans in `related_locations`

Open fcasal opened this issue 1 year ago • 0 comments

This PR collects clippy's diagnostic children span locations into the SARIF related locations.

This prevents clippy-sarif from removing relevant data that is present in clippy's json output.

As an example, running cargo clippy with cargo clippy --message-format=json on the following file

fn main() {
    let i = 3;
    let x = format!("{}", i);
}

generates a help diagnostic

{
                "children": [],
                "code": null,
                "level": "help",
                "message": "if this is intentional, prefix it with an underscore",
                "rendered": null,
                "spans": [
                    {
                        "byte_end": 36,
                        "byte_start": 35,
                        "column_end": 10,
                        "column_start": 9,
                        "expansion": null,
                        "file_name": "sarif-rs-example/src/main.rs",
                        "is_primary": true,
                        "label": null,
                        "line_end": 3,
                        "line_start": 3,
                        "suggested_replacement": "_x",
                        "suggestion_applicability": "MachineApplicable",
                        "text": [
                            {
                                "highlight_end": 10,
                                "highlight_start": 9,
                                "text": "    let x = format!(\"{}\", i);"
                            }
                        ]
                    }
                ]
            }

However, neither the message nor the suggestion shows up in the SARIF file:

"results": [
        {
          "level": "warning",
          "locations": [
            {
              "physicalLocation": {
                "artifactLocation": {
                  "uri": "sarif-rs-example/src/main.rs"
                },
                "region": {
                  "byteLength": 1,
                  "byteOffset": 35,
                  "endColumn": 10,
                  "endLine": 3,
                  "startColumn": 9,
                  "startLine": 3
                }
              }
            }
          ],
          "message": {
            "text": "unused variable: `x`"
          },
          "ruleId": "unused_variables",
          "ruleIndex": 0
        }
      ],

This PR adds a related location entry with this information:

"relatedLocations": [
            {
              "message": {
                "text": "if this is intentional, prefix it with an underscore \"_x\""
              },
              "physicalLocation": {
                "artifactLocation": {
                  "uri": "sarif-rs-example/src/main.rs"
                },
                "region": {
                  "byteLength": 1,
                  "byteOffset": 35,
                  "endColumn": 10,
                  "endLine": 3,
                  "startColumn": 9,
                  "startLine": 3
                }
              }
            }
          ],
          "ruleId": "unused_variables",
          "ruleIndex": 0
        }

fcasal avatar Jan 23 '24 15:01 fcasal