diff --git a/src/extension.ts b/src/extension.ts index 1c6a0e3..4c22832 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -24,7 +24,8 @@ const criticalWarningTypes = [ 'preprocessorErrorDirective', 'syntaxError', 'unhandledChar', - 'unknownMacro' + 'unknownMacro', + 'checkersReport' ]; function parseSeverity(str: string): vscode.DiagnosticSeverity { @@ -270,19 +271,19 @@ async function runCppcheckOnFileXML( for (const e of errors) { const isCriticalError = criticalWarningTypes.includes(e.$.id); const locations = e.location || []; - if (!locations.length) { + if (!isCriticalError && !locations.length) { continue; } - const mainLoc = locations[locations.length - 1].$; + const mainLoc = locations.length ? locations[locations.length - 1]?.$ : []; // If main location is not current file, then skip displaying warning unless it is critical - if (!isCriticalError && !filePath.endsWith(mainLoc.file)) { + if (!isCriticalError && !filePath.endsWith(mainLoc?.file)) { continue; } // Cppcheck line number is 1-indexed, while VS Code uses 0-indexing - let line = Number(mainLoc.line) - 1; + let line = Number(mainLoc?.line ?? 0) - 1; // Invalid line number usually means non-analysis output if (isNaN(line) || line < 0 || line >= document.lineCount) { if (isCriticalError) { @@ -293,7 +294,7 @@ async function runCppcheckOnFileXML( } // Cppcheck col number is 1-indexed, while VS Code uses 0-indexing - let col = Number(mainLoc.column) - 1; + let col = Number(mainLoc?.column ?? 0) - 1; if (isNaN(col) || col < 0 || col > document.lineAt(line).text.length) { col = 0; }