tfussell / xlnt

:bar_chart: Cross-platform user-friendly xlsx library for C++11+
Other
1.5k stars 423 forks source link

warning: control reaches end of non-void function [-Wreturn-type] #90

Closed sukoi26 closed 7 years ago

sukoi26 commented 8 years ago

i note a lot of warning for that as for files - custom_value_traits.hpp and cpp It's a good exercise for me to analyze your code in order to use the C ++ , to improve my apprentice level .

    static std::string serialize (xlnt::pane_corner corner, const serializer &)
    {
        switch (corner)
        {
        case xlnt::pane_corner::bottom_left: return "bottomLeft";
        case xlnt::pane_corner::bottom_right: return "bottomRight";
        case xlnt::pane_corner::top_left: return "topLeft";
        case xlnt::pane_corner::top_right: return "topRight";
        }
}

it miss a return after the switch instruction , it's an evidence the solving is perhaps unnecessary in this code but , it gives a code easier to debug , suggest


    static std::string serialize (xlnt::pane_corner corner, const serializer &)
    {        
    switch (corner)
        {
        case xlnt::pane_corner::bottom_left: return "bottomLeft";
        case xlnt::pane_corner::bottom_right: return "bottomRight";
        case xlnt::pane_corner::top_left: return "topLeft";
        case xlnt::pane_corner::top_right: return "topRight";
        }
    return "not found";
    }

OR something better

tfussell commented 8 years ago

Good catch. The problem is that other compilers have a warning about "unreachable code". If every switch case is handled, the compiler detects that the return statement can't be reached. I have to suppress this warning or use the preprocessor to only have that return statement on certain compilers. Something like this:

static std::string serialize (xlnt::pane_corner corner, const serializer &)
{        
    switch (corner)
    {
        case xlnt::pane_corner::bottom_left: return "bottomLeft";
        case xlnt::pane_corner::bottom_right: return "bottomRight";
        case xlnt::pane_corner::top_left: return "topLeft";
        case xlnt::pane_corner::top_right: return "topRight";
    }
#ifdef ALLOW_RETURN_AFTER_SWITCH
    return "not found";
#endif
}
sukoi26 commented 8 years ago

for GCC - compilers -Wall, and we cannot no-warn the return-type

-Wall
    This enables all the warnings about constructions that some users consider questionable, and that are easy to avoid (or modify to prevent the warning), even in conjunction with macros. This also enables some language-specific warnings described in C++ Dialect Options and Objective-C and Objective-C++ Dialect Options.

    -Wall turns on the following warning flags:

              -Waddress   
              -Warray-bounds (only with -O2)  
              -Wc++11-compat  
              -Wchar-subscripts  
              -Wenum-compare (in C/Objc; this is on by default in C++) 
              -Wimplicit-int (C and Objective-C only) 
              -Wimplicit-function-declaration (C and Objective-C only) 
              -Wcomment  
              -Wformat   
              -Wmain (only for C/ObjC and unless -ffreestanding)  
              -Wmaybe-uninitialized 
              -Wmissing-braces  
              -Wnonnull  
              -Wparentheses  
              -Wpointer-sign  
              -Wreorder   
              -Wreturn-type  
              -Wsequence-point  
              -Wsign-compare (only in C++)  
              -Wstrict-aliasing  
              -Wstrict-overflow=1  
              -Wswitch  
              -Wtrigraphs  
              -Wuninitialized  
              -Wunknown-pragmas  
              -Wunused-function  
              -Wunused-label     
              -Wunused-value     
              -Wunused-variable  
              -Wvolatile-register-var 
OR , as to rearrange the code to avoid the case "default" as  the using of a dictionary map or something like that ( dictionary of pane_corner defines all case ) and the code become 
  static std::string serialize (xlnt::pane_corner corner, const serializer &)
    {        
        return dictionary_pane_corner.find(corner);
    }   
tfussell commented 8 years ago

The unordered_map solution doesn't seem bad. I'll see how much work that would be and if there are any downsides. Thanks for the contribution.

sukoi26 commented 7 years ago

a test code, note the cast to int not so good ...

static std::string serialize (xlnt::pane_corner corner, const serializer &)
    {        
    return (std::string [])
        {
        "bottomLeft",
        "bottomRight",
        "topLeft",
        "topRight",
        }[(int)corner];
    }
tfussell commented 7 years ago

Looks to be fixed according to gcc 6.2.