Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMap contains() + value() - better way?
QtWS25 Last Chance

QMap contains() + value() - better way?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 659 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vadi2
    wrote on last edited by
    #1

    I've got the following bit of code:

    if (dockWindowConsoleMap.contains(windowName)) {
      font = dockWindowConsoleMap.value(windowName)->mUpperPane->mDisplayFont;
    

    It's tidy, it's safe, but my static analyser complains about the impossible case that I'm doing a null pointer dereference with value() because of the default-constructed argument. It's an impossible case because of the contains() check but it's not smart enough.

    I don't want to mark every time I use this pattern as a false positive in the static analyser - what's a better way to get a value from a QMap that's static-analysis friendly?

    kshegunovK 1 Reply Last reply
    0
    • V Vadi2

      I've got the following bit of code:

      if (dockWindowConsoleMap.contains(windowName)) {
        font = dockWindowConsoleMap.value(windowName)->mUpperPane->mDisplayFont;
      

      It's tidy, it's safe, but my static analyser complains about the impossible case that I'm doing a null pointer dereference with value() because of the default-constructed argument. It's an impossible case because of the contains() check but it's not smart enough.

      I don't want to mark every time I use this pattern as a false positive in the static analyser - what's a better way to get a value from a QMap that's static-analysis friendly?

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by VRonin
      #2

      You can try with QMap::find() and then QMap::Iterator::value() if the returned iterator isn't equal to QMap::end():

      QMap< ... >::Iterator element = dockWindowConsoleMap.find(windowName);
      if (element != dockWindowConsoleMap.end())
           font = element.value()->mUpperPane->mDisplayFont;
      

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        While @kshegunov suggests a correct efficency improvement, I doubt it will solve your problem as it's triggered by mUpperPane-> not what's on the left. Q_ASSERT should solve it though.

        const auto element = dockWindowConsoleMap.constFind(windowName);
        if (element != dockWindowConsoleMap.cend()){
        Q_ASSERT(element.value()->mUpperPane);
        font = element.value()->mUpperPane->mDisplayFont;
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        4
        • V Offline
          V Offline
          Vadi2
          wrote on last edited by
          #4

          I see. Thank you both for the responses.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved