Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/util/codeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,24 @@ export class CodeActionProvider implements vscode.CodeActionProvider {
// Copy indentation from line affected by diagnostic
const indent = lineText.match(/^\s*/)?.[0] ?? "";

var affectedLine = diagnostic.range.start.line;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we check somewhere that the first location is in the same file? the locations for a warning can point at different files.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

very good point! Fixed

var documentUri = document.uri;

// Due to a quirk in cppcheck, even though last location for a multiple location warning is considered 'main location',
// inline suppression should be added to the 1st location (reversed order in diagnostic relatedInformation)
const multipleLocationWarning = (diagnostic.relatedInformation?.length ?? 0 ) > 0;
if (multipleLocationWarning && diagnostic?.relatedInformation?.[0]) {
const lastLocation = diagnostic?.relatedInformation?.[diagnostic?.relatedInformation?.length - 1].location;
documentUri = lastLocation.uri;
affectedLine = lastLocation.range.start.line;
}

// Insert suppression comment above affected line
const suppressLineEdit = new vscode.WorkspaceEdit();
suppressLineEdit.insert(
document.uri,
documentUri,
new vscode.Position(
diagnostic.range.start.line,
affectedLine,
0,
),
`${indent}// cppcheck-suppress ${diagnosticCode}\n`
Expand Down
Loading