This is a fairly hypothetical scenario and I don't think there's good caselaw on this. Obviously, the source code does contain the third party code and is a derivative work. Thus, the source code licensing will have to comply with the third party license.
However, there is a good argument that any binaries built from the source code will not be derivative of the third party code. Then, the binary licensing is independent from the third party license. This is not self-evident though, since the binary is usually just a different format for the source code. If this scenario was to be discussed in court, it would likely need an expert witness to explain how the compilation process only translates some parts of the input, and that the binary in question does not contain any of this third party code.
A project can make it much more obvious whether third party code is included by moving the third party code into separate files or plugins, and potentially loading them as necessary. By deleting the third party files and getting the essentially same compilation output, it can be more easily demonstrated that the third party code is not being used. It is also easier to manage licensing on a per-file basis, especially if you add machine-readable license identifiers to every file, or at least have a separate license file for a directory with third party content.