Skip to content

Fix compiler warnings #191

Closed
Closed
@maxbla

Description

@maxbla

if you check the box "show verbose output during compilation" in the Arduino IDE, verifying any library that includes RTClib will print warnings.

RTClib.cpp:543:7: warning: 'isPM' may be used uninitialized in this function [-Wmaybe-uninitialized]
       if (isPM) {
       ^
RTClib.cpp:468:28: note: 'isPM' was declared here
   uint8_t hourReformatted, isPM;
                            ^
RTClib.cpp:492:47: warning: 'hourReformatted' may be used uninitialized in this function [-Wmaybe-uninitialized]
         buffer[i + 1] = '0' + hourReformatted % 10;
                                               ^
RTClib.cpp:468:11: note: 'hourReformatted' was declared here
   uint8_t hourReformatted, isPM;

It would be better if compiling didn't print warnings, but as far as I can tell, the uninitalized values aren't being read. The warnings are just being overly careful.

Easy fix: change

  uint8_t hourReformatted, isPM;
  if (apTag) {     // 12 Hour Mode
    if (hh == 0) { // midnight
      isPM = false;
      hourReformatted = 12;
    } else if (hh == 12) { // noon
      isPM = true;
      hourReformatted = 12;
    } else if (hh < 12) { // morning
      isPM = false;
      hourReformatted = hh;
    } else { // 1 o'clock or after
      isPM = true;
      hourReformatted = hh - 12;
    }
  }

to

  uint8_t hourReformatted = hh;
  bool isPM = hh >= 12;
  if (apTag) {     // 12 Hour Mode
    if (hh == 0) { // midnight
      hourReformatted = 12;
    } else if (hh <= 12) { // morning/noon
      hourReformatted = hh;
    } else { // 1 o'clock or after
      hourReformatted = hh - 12;
    }
  }

I can submit a PR making this change, but I'd prefer to wait until my first PR, #190 goes through.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions