bblanchon / ArduinoJson

📟 JSON library for Arduino and embedded C++. Simple and efficient.
https://arduinojson.org
MIT License
6.63k stars 1.1k forks source link

Lint error ovl_no_viable_subscript has occured #2104

Closed ShortArrow closed 3 weeks ago

ShortArrow commented 3 weeks ago

image

Describe the issue
Lint error has occured. Compile process is no issue.

Troubleshooter report

No viable overloaded operator[] for type 'JsonDocument', clang(ovl_no_viable_subscript).
Type 'JsonObject' does not provide a subscript operator, clang(ovl_no_oper).
No matching function for call to 'serializeJson'
No matching function for call to 'serializeJson', clang(ovl_no_viable_function_in_call).
JsonSerializer.hpp(131, 8): Candidate function template not viable: no known conversion from 'JsonDocument' to 'JsonVariantConst' for 1st argument
JsonSerializer.hpp(138, 15): Candidate function not viable: requires 3 arguments, but 2 were provided

Environment
Here is the environment that I'm using':

Reproduction
Here is a small snippet that demonstrate the problem.

#include <ArduinoJson.hpp>
#include "Arduino.h"
#include "ArduinoJson/Array/JsonArray.hpp"
#include "ArduinoJson/Document/JsonDocument.hpp"
#include "ArduinoJson/Object/JsonObject.hpp"

void CanbusToJsonSerial(String label, int value) {
  JsonDocument doc;
  JsonObject data = doc["data"].to<JsonObject>();
  doc["topic"] = "CanbusA";
  data[label] = value;
  doc.shrinkToFit();
  serializeJson(doc, Serial);
  Serial.println();
}

arduino-cli.yaml

board_manager:
  additional_urls: []
build_cache:
  compilations_before_purge: 10
  ttl: 720h0m0s
daemon:
  port: "COM5"
directories:
  data: Arduino
  downloads: Arduino/staging
  user: Arduino
library:
  enable_unsafe_install: false
logging:
  file: ""
  format: text
  level: info
metrics:
  addr: :9090
  enabled: true
output:
  no_color: false
sketch:
  always_export_binaries: false
updater:
  enable_notification: true
fqbn: arduino:renesas_uno:minima

.clangd

CompileFlags:
  Add: [
    "-I./**",
    "-I./Arduino/data/packages/arduino/tools/arm-none-eabi-gcc/**",
    "-I./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/variants/MINIMA/includes/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/variants/MINIMA/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/libraries/Arduino_CAN/src/**",
    "-I./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/Arduino_FreeRTOS/src/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/libraries/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/cores/arduino/api/tinyusb/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/cores/arduino/api/deprecated/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/cores/arduino/api/deprecated-avr-comp/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/cores/arduino/api/**",
    "-I./Arduino/data/packages/arduino/hardware/renesas_uno/1.2.0/cores/arduino/**",
    "-I./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/**",
    "-I./Arduino/data/packages/arduino/tools/arm-none-eabi-gcc/7-2017q4/arm-none-eabi/include/**",
    "-I./Arduino/**",
  ]

Program output

[{
    "resource": "./target.cpp",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "ovl_no_viable_subscript",
    "severity": 8,
    "message": "No viable overloaded operator[] for type 'JsonDocument'",
    "source": "clang",
    "startLineNumber": 391,
    "startColumn": 24,
    "endLineNumber": 391,
    "endColumn": 25
},{
    "resource": "./target.cpp",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "ovl_no_viable_subscript",
    "severity": 8,
    "message": "No viable overloaded operator[] for type 'JsonDocument'",
    "source": "clang",
    "startLineNumber": 392,
    "startColumn": 6,
    "endLineNumber": 392,
    "endColumn": 7
},{
    "resource": "./target.cpp",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "ovl_no_oper",
    "severity": 8,
    "message": "Type 'JsonObject' does not provide a subscript operator",
    "source": "clang",
    "startLineNumber": 393,
    "startColumn": 7,
    "endLineNumber": 393,
    "endColumn": 8
},{
    "resource": "./target.cpp",
    "owner": "_generated_diagnostic_collection_name_#0",
    "code": "ovl_no_viable_function_in_call",
    "severity": 8,
    "message": "No matching function for call to 'serializeJson'",
    "source": "clang",
    "startLineNumber": 395,
    "startColumn": 3,
    "endLineNumber": 395,
    "endColumn": 16,
    "relatedInformation": [
        {
            "startLineNumber": 131,
            "startColumn": 8,
            "endLineNumber": 131,
            "endColumn": 21,
            "message": "Candidate function template not viable: no known conversion from 'JsonDocument' to 'JsonVariantConst' for 1st argument",
            "resource": "./Arduino/libraries/ArduinoJson/src/ArduinoJson/Json/JsonSerializer.hpp"
        },
        {
            "startLineNumber": 138,
            "startColumn": 15,
            "endLineNumber": 138,
            "endColumn": 28,
            "message": "Candidate function not viable: requires 3 arguments, but 2 were provided",
            "resource": "./Arduino/libraries/ArduinoJson/src/ArduinoJson/Json/JsonSerializer.hpp"
        }
    ]
}]

compile output

INFO[0000] Using config file: ./arduino-cli.yaml
INFO[0000] arduino-cli version 0.35.3-arch
INFO[0000] Executing `arduino-cli compile`
INFO[0000] Reading sketch from dir: .
INFO[0000] Checking signature                            index=Arduino/package_index.json signature
INFO[0000] Loading hardware from: Arduino/packages
INFO[0000] Loading package arduino from: ./Arduino/packages/ar
INFO[0000] Missing signature file                        index=/home/who/Documents/GitHub/RoofPaint
INFO[0000] Adding monitor tool                           protocol=serial tool="builtin:serial-monit
INFO[0000] Loaded platform                               platform="arduino:renesas_uno@1.2.0"
INFO[0000] Checking existence of 'tools' path: ./Arduino/packa
INFO[0000] Loading tools from dir: ./Arduino/packages/arduino/
INFO[0000] Loaded tool                                   tool="arduino:arm-none-eabi-gcc@7-2017q4"
INFO[0000] Loaded tool                                   tool="arduino:bossac@1.9.1-arduino5"
INFO[0000] Loaded tool                                   tool="arduino:dfu-util@0.11.0-arduino5"
INFO[0000] Loaded tool                                   tool="arduino:openocd@0.11.0-arduino2"
INFO[0000] Loading package builtin from: ./Arduino/packages/bu
INFO[0000] Checking existence of 'tools' path: ./Arduino/packa
INFO[0000] Loading tools from dir: ./Arduino/packages/builtin/
INFO[0000] Loaded tool                                   tool="builtin:ctags@5.8-arduino11"
INFO[0000] Loaded tool                                   tool="builtin:dfu-discovery@0.1.2"
INFO[0000] Loaded tool                                   tool="builtin:mdns-discovery@1.0.9"
INFO[0000] Loaded tool                                   tool="builtin:serial-discovery@1.4.1"
INFO[0000] Loaded tool                                   tool="builtin:serial-monitor@0.14.1"
INFO[0000] Adding libraries dir                          dir=.
INFO[0000] Loading libraries index file                  index=Arduino/library_index.json
INFO[0000] Adding libraries dir                          dir=Arduino/libraries location=user
INFO[0000] Adding libraries dir                          dir=.
INFO[0000] Adding libraries dir                          dir=.
INFO[0000] Resolving include EEPROM.h for arch renesas_uno
INFO[0000]   found better lib                            lib=EEPROM prio=6B0
INFO[0000] Resolving include virtualEEPROM.h for arch renesas_uno
INFO[0000]   found better lib                            lib=BlockDevices prio=3EA
INFO[0000] Resolving include Arduino_CAN.h for arch renesas_uno
INFO[0000]   found better lib                            lib=Arduino_CAN prio=6B0
INFO[0000] Resolving include ArduinoJson.h for arch renesas_uno
INFO[0000]   found better lib                            lib=ArduinoJson prio=6A7
INFO[0000] Resolving include storage_retarget.h for arch renesas_uno
INFO[0000]   found better lib                            lib=Storage prio=3EA
INFO[0000] Resolving include Arduino_FreeRTOS.h for arch renesas_uno
INFO[0000]   found better lib                            lib=Arduino_FreeRTOS prio=6B0
INFO[0000] Resolving include SoftwareSerial.h for arch renesas_uno
INFO[0000]   found better lib                            lib=SoftwareSerial prio=6B0
Sketch uses 71892 bytes (27%) of program storage space. Maximum is 262144 bytes.
Global variables use 17572 bytes (53%) of dynamic memory, leaving 15196 bytes for local variables. Maximum is 32768 bytes.

Used library     Version Path
EEPROM           1.0     ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/EEPROM
BlockDevices             ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/BlockDevices
Arduino_CAN      1.0     ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/Arduino_CAN
ArduinoJson      7.0.4   ./Arduino/libraries/ArduinoJson
Storage                  ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/Storage
Arduino_FreeRTOS 1.0.0   ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/Arduino_FreeRTOS
SoftwareSerial   1.0     ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0/libraries/SoftwareSerial

Used platform       Version Path
arduino:renesas_uno 1.2.0   ./Arduino/packages/arduino/hardware/renesas_uno/1.2.0
bblanchon commented 3 weeks ago

Hi @ShortArrow,

This is a problem with the linter, not with the library. What do you expect from me?

Best regards, Benoit

ShortArrow commented 3 weeks ago

@bblanchon Thank you. I realized the problem was that the linter failed to detect operator overloading. Just being able to realize that was a huge win for me.

I'm not very good at cpp, so I couldn't read deep into the library. So big thanks for you.