Invalid if() expression silently accepted
The following block of code prints nothing instead of flagging the if()
command as containing an invalid expression:
if(NOT STREQUAL "Something") # This seems like it should cause a fatal error
message(FATAL_ERROR "We don't get here")
endif()
While code like the above is fairly obviously questionable, it is likely to be encountered through a more subtle problem:
set(someVar "") # User might have been expecting this to have a non-empty value
if(NOT ${someVar} STREQUAL "Something") # Note the lack of quotes around ${someVar}
message(FATAL_ERROR "We don't get here")
endif()
The above should have put quotes around ${someVar}
to ensure it handles an empty string. But the code as written should ideally be caught by the argument processing of the if()
command, since NOT
is a keyword, which means what follows will be an invalid expression because there is no argument on the left of the STREQUAL
comparison. Maybe this is technically valid after all, but it seems like it should be an error to me. I encountered this exact scenario in some production work, and it was not at all obvious because the variable in question is defined but empty for one particular platform (VCPKG_CMAKE_SYSTEM_NAME
, which is empty on Windows for historical reasons).