I have read this link so have a basic understanding of what the warning refers to.
When I run code analysis on my MFC project I get a barrage of these warnings:
d:\my programs\2017\meetschedassist\meeting schedule assistant\synchexclusionsdlg.cpp(295): warning C6031: Return value ignored: 'ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::LoadStringW'.
d:\my programs\2017\meetschedassist\meeting schedule assistant\synchexclusionsdlg.cpp(297): warning C6031: Return value ignored: 'ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t> > >::LoadStringW'.
So, for example, it complains about this code snippet:
if (iImage == IMG_CHECKED)
strText.LoadString(IDS_STR_YES);
else
strText.LoadString(IDS_STR_NO);
I have read the help documentation for LoadString and ironically their example does this:
CAtlString s;
s.LoadString(IDS_APP_TITLE);
They don't test the return value either. :)
Now, I realise that I could try to fix my code and test the return values - which will take me a very long time! And I realise that I can just subconsciously ignore these warnings.
But is it possible to supress this specific warning (concerning C6031 CString::LoadString
) during analysis?
Update
I have tried adding this to my stdafx.h
(based on comments):
#pragma warning( disable : 6031)
It certainly works. But I was hoping to just supress 6031 errors for CString::LoadString
.
Your code ignores the return value from the function, which returns
BOOL
. The warning is correct. You can turn off that warning, but doing so (as you've discovered) turns off the warning completely for a compilation unit.You can use #pragma warning(suppress) to turn off the warning for a single invocation of the warning.
But I think the smart thing to do would be to write a wrapper function that loads a string. That wrapper checks the return value from the function and reacts appropriately if the function returns FALSE because the string isn't found. What you think is appropriate is completely up to you: maybe you log an error, maybe you pop up a message box, maybe you use a default string.