Esempio n. 1
0
 /** adds a message for a re-import */
 public void addReimportMessage(Found f) {
   for (GenAndTok g : f) {
     // we don't want to add reimport messages if they are found in a wild import
     if (g.generator instanceof SourceToken
         && !(g.generator instanceof ImportPartSourceToken)
         && AbstractVisitor.isWildImport(g.generator) == false) {
       addMessage(IAnalysisPreferences.TYPE_REIMPORT, g.generator, g.tok);
     }
   }
 }
Esempio n. 2
0
  /**
   * adds a message for something that was not used
   *
   * @param node the node representing the scope being closed when adding the unused message
   */
  public void addUnusedMessage(SimpleNode node, Found f) {
    for (GenAndTok g : f) {
      if (g.generator instanceof SourceToken) {
        SimpleNode ast = ((SourceToken) g.generator).getAst();

        // it can be an unused import
        if (ast instanceof Import || ast instanceof ImportFrom) {
          if (AbstractVisitor.isWildImport(ast)) {
            addMessage(IAnalysisPreferences.TYPE_UNUSED_WILD_IMPORT, g.generator, g.tok);
          } else if (!(g.generator instanceof ImportPartSourceToken)) {
            addMessage(IAnalysisPreferences.TYPE_UNUSED_IMPORT, g.generator, g.tok);
          }
          continue; // finish it...
        }
      }

      // or unused variable
      // we have to check if this is a name we should ignore
      if (startsWithNamesToIgnore(g)) {
        int type = IAnalysisPreferences.TYPE_UNUSED_VARIABLE;

        if (g.tok instanceof SourceToken) {
          SourceToken t = (SourceToken) g.tok;
          SimpleNode ast = t.getAst();
          if (ast instanceof NameTok) {
            NameTok n = (NameTok) ast;
            if (n.ctx == NameTok.KwArg || n.ctx == NameTok.VarArg || n.ctx == NameTok.KeywordName) {
              type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
            }
          } else if (ast instanceof Name) {
            Name n = (Name) ast;
            if (n.ctx == Name.Param || n.ctx == Name.KwOnlyParam) {
              type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
            }
          }
        }
        boolean addMessage = true;
        if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER) {
          // just add unused parameters in methods that have some content (not only 'pass' and
          // 'strings')

          if (node instanceof FunctionDef) {
            addMessage = false;
            FunctionDef def = (FunctionDef) node;
            for (stmtType b : def.body) {
              if (b instanceof Pass) {
                continue;
              }
              if (b instanceof Expr) {
                Expr expr = (Expr) b;
                if (expr.value instanceof Str) {
                  continue;
                }
              }
              addMessage = true;
              break;
            }
          }
        } // END if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER)

        if (addMessage) {
          addMessage(type, g.generator, g.tok);
        }
      }
    }
  }