5

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.

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 3
    Use `#pragma warning`. http://stackoverflow.com/questions/7159348/disable-single-warning-error – Paul Rooney Dec 19 '16 at 09:38
  • @PaulRooney Thanks, but how do I use that to specifically supress that warning in that context? And, how do I apply that across the whole solution? – Andrew Truckle Dec 19 '16 at 09:42
  • @PaulRooney Thanks. Just looked that up. But `/w` on it's own is going to supress all warnings which is not what I want. – Andrew Truckle Dec 19 '16 at 09:49
  • 1
    You have linked to the Windows API call [LoadString](https://msdn.microsoft.com/en-us/library/windows/desktop/ms647486.aspx), but your code uses [CStringT::LoadString](https://msdn.microsoft.com/en-us/library/a44fb3wy.aspx). Is this intentional? – IInspectable Dec 19 '16 at 11:36
  • @IInspectable I have adjusted the question. – Andrew Truckle Dec 19 '16 at 11:43
  • 2
    You have linked to the `CString::LoadString` implementation of Visual Studio 6, but you are using Visual Studio 2017 RC. The implementation you are using is (presumably) [CStringT::LoadString](https://msdn.microsoft.com/en-us/library/a44fb3wy.aspx). – IInspectable Dec 19 '16 at 12:12
  • @IInspectable Further revision made. – Andrew Truckle Dec 19 '16 at 12:14

1 Answers1

4

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.

MikeB
  • 1,452
  • 14
  • 28
  • Thinking about it one could wrap the call with VERIFY. See: https://learn.microsoft.com/en-us/cpp/build/using-verify-instead-of-assert?view=vs-2019 – Andrew Truckle Dec 12 '19 at 10:45