False-positive note "Local variable is assigned but never used"
Summary
FPC complains project1.lpr(11,3) Note: Local variable "ErrorType" is assigned but never used for the following code, although this variable is actually used.
Duplicate of #26002?
System Information
- Operating system: Windows 7 Ultimate x64
- Processor architecture: x86-64
- Compiler version: 3.2.2
- Device: Computer
Example Project
program Project1;
{$MODE OBJFPC}
uses SysUtils;
// NB: be careful, there's already a typed const ErrorCode in systemh.inc
// {$WRITEABLECONST OFF} doesn't help as it impacts only the constant itself, not the usage of it
var
ErrorType: TClass;
ErrorCode: LongInt;
begin
try
WriteLn('kek');
except on E: TObject do
begin
if E is Exception then
begin
// gather some additional info from all standard descendants known
if E is EVariantError then ErrorCode := EVariantError(E).ErrCode
else if E is EInOutError then ErrorCode := EInOutError(E).ErrorCode
else if E is EOSError then ErrorCode := EOSError(E).ErrorCode;
WriteLn(ErrorCode);
ErrorType := Exception;
end
else
begin
ErrorType := TObject;
end;
// also write string representation if available (check if method is overridden)
if @E.ToString <> @ErrorType.ToString then // <-- using ErrorType here
WriteLn(E.ToString());
end;
end;
end.
Relevant logs and/or screenshots
\project1>C:\Lazarus\fpc\3.2.2\bin\x86_64-win64\fpc Project1.lpr
Free Pascal Compiler version 3.2.2 [2025/07/19] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Win64 for x64
Compiling Project1.lpr
Project1.lpr(11,3) Note: Local variable "ErrorType" is assigned but never used
Linking Project1.exe
40 lines compiled, 0.2 sec, 72704 bytes code, 5252 bytes data
1 note(s) issued
Edited by Dmitry D. Chernov