• Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    22
    ·
    1 year ago

    A few years ago, all the languages I would use started to have automatic unused variable warnings built-in. And yeah, by now when I hear of people that don’t have that, it’s very much a feeling of “Man, you live like this?”.

  • Avid Amoeba@lemmy.ca
    link
    fedilink
    arrow-up
    23
    arrow-down
    3
    ·
    edit-2
    1 year ago

    Scary indeed.

    This one could be helped by always using this pattern whenever you write a function that returns a value, in any language, along with no early returns:

    int func(...) {
        int result = -1;
        ...
        return result;
    }
    

    I always start with writing my result default value, ideally indicating failure, and the return line. Then I implement the rest. We often don’t have the luxury of choosing the language we work with that has the features we like, but consistently enforced code style can help with a lot of problems. Anyone can make mistakes like the one in this bug regardless of experience so every little bit helps.

  • DapperPenguin@programming.dev
    link
    fedilink
    arrow-up
    13
    arrow-down
    1
    ·
    edit-2
    1 year ago

    I appreciate rust as much as the next dev. But you can define your own types in C just as well? And with the proper warnings and errors -Wall -Werror in place, any warning is an error, and implicit conversions should probably be a warning right?

    ETA: Just tried with the following C code and could not get it to fail with gcc.

    typedef int t_0;
    typedef long t_1;
    
    t_0 test() {
      t_1 foo = 1;
      return foo;
    }
    

    Tried with gcc -Wall -Wextra -Wpedantic -Werror and it compiled just fine.

    • brisk@aussie.zone
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      typedef in C just make an alias to the same type. structs have nominal typing though:

      // this typedef is optional to avoid having to refer to the struct tag when referencing the types
      typedef struct {int} t_0;
      typedef struct {long} t_1;
      
      t_0 test() {
        t_1 foo = {1};
        return foo; // error
      }