How can I lint C++ code to find all unused return values?












8















I would like to statically inspect all calls to non-void functions where the return value is not used.



In effect this would be like applying __attribute__ ((warn_unused_result)) to all non-void functions, but of course for a large project that is not practical to do.



Is there any static analysis tool that can provide this information?










share|improve this question




















  • 1





    compile with gcc -Wall , you are gonna get warnings for all those occurances

    – RoiHatam
    May 5 '17 at 18:52








  • 2





    @Roi No, you won't get any warnings about that.

    – Neil Butterworth
    May 5 '17 at 18:55






  • 1





    That's not an unused return value, it's no value being returned. The two things are completely different.

    – Neil Butterworth
    May 5 '17 at 18:59








  • 2





    @RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

    – Galik
    May 5 '17 at 19:00






  • 1





    That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

    – too honest for this site
    May 5 '17 at 21:22


















8















I would like to statically inspect all calls to non-void functions where the return value is not used.



In effect this would be like applying __attribute__ ((warn_unused_result)) to all non-void functions, but of course for a large project that is not practical to do.



Is there any static analysis tool that can provide this information?










share|improve this question




















  • 1





    compile with gcc -Wall , you are gonna get warnings for all those occurances

    – RoiHatam
    May 5 '17 at 18:52








  • 2





    @Roi No, you won't get any warnings about that.

    – Neil Butterworth
    May 5 '17 at 18:55






  • 1





    That's not an unused return value, it's no value being returned. The two things are completely different.

    – Neil Butterworth
    May 5 '17 at 18:59








  • 2





    @RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

    – Galik
    May 5 '17 at 19:00






  • 1





    That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

    – too honest for this site
    May 5 '17 at 21:22
















8












8








8


1






I would like to statically inspect all calls to non-void functions where the return value is not used.



In effect this would be like applying __attribute__ ((warn_unused_result)) to all non-void functions, but of course for a large project that is not practical to do.



Is there any static analysis tool that can provide this information?










share|improve this question
















I would like to statically inspect all calls to non-void functions where the return value is not used.



In effect this would be like applying __attribute__ ((warn_unused_result)) to all non-void functions, but of course for a large project that is not practical to do.



Is there any static analysis tool that can provide this information?







c++ static-analysis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 5 '17 at 21:58







Nicholas Bishop

















asked May 5 '17 at 18:51









Nicholas BishopNicholas Bishop

609513




609513








  • 1





    compile with gcc -Wall , you are gonna get warnings for all those occurances

    – RoiHatam
    May 5 '17 at 18:52








  • 2





    @Roi No, you won't get any warnings about that.

    – Neil Butterworth
    May 5 '17 at 18:55






  • 1





    That's not an unused return value, it's no value being returned. The two things are completely different.

    – Neil Butterworth
    May 5 '17 at 18:59








  • 2





    @RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

    – Galik
    May 5 '17 at 19:00






  • 1





    That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

    – too honest for this site
    May 5 '17 at 21:22
















  • 1





    compile with gcc -Wall , you are gonna get warnings for all those occurances

    – RoiHatam
    May 5 '17 at 18:52








  • 2





    @Roi No, you won't get any warnings about that.

    – Neil Butterworth
    May 5 '17 at 18:55






  • 1





    That's not an unused return value, it's no value being returned. The two things are completely different.

    – Neil Butterworth
    May 5 '17 at 18:59








  • 2





    @RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

    – Galik
    May 5 '17 at 19:00






  • 1





    That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

    – too honest for this site
    May 5 '17 at 21:22










1




1





compile with gcc -Wall , you are gonna get warnings for all those occurances

– RoiHatam
May 5 '17 at 18:52







compile with gcc -Wall , you are gonna get warnings for all those occurances

– RoiHatam
May 5 '17 at 18:52






2




2





@Roi No, you won't get any warnings about that.

– Neil Butterworth
May 5 '17 at 18:55





@Roi No, you won't get any warnings about that.

– Neil Butterworth
May 5 '17 at 18:55




1




1





That's not an unused return value, it's no value being returned. The two things are completely different.

– Neil Butterworth
May 5 '17 at 18:59







That's not an unused return value, it's no value being returned. The two things are completely different.

– Neil Butterworth
May 5 '17 at 18:59






2




2





@RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

– Galik
May 5 '17 at 19:00





@RoiHatam That's something different. That when the function itself forget to return a value. What we have here is when the caller of the function doesn't use its returned value.

– Galik
May 5 '17 at 19:00




1




1





That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

– too honest for this site
May 5 '17 at 21:22







That's quite useless. For printf for example one typically does not test the result and there are a lot of other functions one does not use the result, although they return one. "but of course for a large project that is not practical to do." - That's a wrong presumption. It is of course the best and correct way to do so! Why do you assume it is not possible? For a single function one knows best if the result is more "informative" or always has to be checked.

– too honest for this site
May 5 '17 at 21:22














2 Answers
2






active

oldest

votes


















0














Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see, it also includes a web based report generator.






share|improve this answer































    0














    I think there are software can do this like DevExtreme and in social.msdn.microsoft.com in the answer for this question how-to-get-a-warning-for-an-unused-return-value? they mention that Premium and Ultimate versions of visual studio has some tools.



    Read this:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355715a-5af7-4a2b-8aa0-bc2112eaa911/how-to-get-a-warning-for-an-unused-return-value?forum=vclanguage



    and this mandatory-error-codes-revisited from: http://www.drdobbs.com/cpp/mandatory-error-codes-revisited/191601612






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43811955%2fhow-can-i-lint-c-code-to-find-all-unused-return-values%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see, it also includes a web based report generator.






      share|improve this answer




























        0














        Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see, it also includes a web based report generator.






        share|improve this answer


























          0












          0








          0







          Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see, it also includes a web based report generator.






          share|improve this answer













          Cppcheck is a command-line tool that tries to detect bugs that your C/C++ compiler doesn't see, it also includes a web based report generator.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 18:12









          sbhsbh

          416




          416

























              0














              I think there are software can do this like DevExtreme and in social.msdn.microsoft.com in the answer for this question how-to-get-a-warning-for-an-unused-return-value? they mention that Premium and Ultimate versions of visual studio has some tools.



              Read this:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355715a-5af7-4a2b-8aa0-bc2112eaa911/how-to-get-a-warning-for-an-unused-return-value?forum=vclanguage



              and this mandatory-error-codes-revisited from: http://www.drdobbs.com/cpp/mandatory-error-codes-revisited/191601612






              share|improve this answer




























                0














                I think there are software can do this like DevExtreme and in social.msdn.microsoft.com in the answer for this question how-to-get-a-warning-for-an-unused-return-value? they mention that Premium and Ultimate versions of visual studio has some tools.



                Read this:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355715a-5af7-4a2b-8aa0-bc2112eaa911/how-to-get-a-warning-for-an-unused-return-value?forum=vclanguage



                and this mandatory-error-codes-revisited from: http://www.drdobbs.com/cpp/mandatory-error-codes-revisited/191601612






                share|improve this answer


























                  0












                  0








                  0







                  I think there are software can do this like DevExtreme and in social.msdn.microsoft.com in the answer for this question how-to-get-a-warning-for-an-unused-return-value? they mention that Premium and Ultimate versions of visual studio has some tools.



                  Read this:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355715a-5af7-4a2b-8aa0-bc2112eaa911/how-to-get-a-warning-for-an-unused-return-value?forum=vclanguage



                  and this mandatory-error-codes-revisited from: http://www.drdobbs.com/cpp/mandatory-error-codes-revisited/191601612






                  share|improve this answer













                  I think there are software can do this like DevExtreme and in social.msdn.microsoft.com in the answer for this question how-to-get-a-warning-for-an-unused-return-value? they mention that Premium and Ultimate versions of visual studio has some tools.



                  Read this:https://social.msdn.microsoft.com/Forums/vstudio/en-US/4355715a-5af7-4a2b-8aa0-bc2112eaa911/how-to-get-a-warning-for-an-unused-return-value?forum=vclanguage



                  and this mandatory-error-codes-revisited from: http://www.drdobbs.com/cpp/mandatory-error-codes-revisited/191601612







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 18:27









                  i_thi_th

                  1,1851719




                  1,1851719






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f43811955%2fhow-can-i-lint-c-code-to-find-all-unused-return-values%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      Npm cannot find a required file even through it is in the searched directory

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith