声纳抱怨局部变量的无用分配 [英] Sonar complaining about useless assignment of local variable

查看:105
本文介绍了声纳抱怨局部变量的无用分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中包含以下代码,并将其与Maven集成后,我正在运行SonarQube 5以对其进行代码质量检查.

I have the following piece of code in my program and I am running SonarQube 5 for code quality check on it after integrating it with Maven.

但是,Sonar要求将此无用的分配删除到本地变量会话".

However, Sonar is asking to Remove this useless assignment to local variable "session".

@RequestMapping(value = "/logoff", method = RequestMethod.GET)
public String getLogoffPage(HttpServletRequest request, HttpServletResponse response) {
    logger.info(" Before Log Offf........ " + request.getSession().getId() );
    HttpSession session =request.getSession(true);
    request.getSession().invalidate();
    myApplication.logout();
    SecurityContextHolder.clearContext();
    session=null;                   
    return "login";
}

推荐答案

假设问题是为什么":

您实际使用session 做什么?没事.

What do you actually do with session? Nothing.

HttpSession session =request.getSession(true);  // session assigned
request.getSession().invalidate();         // session NOT used
myApplication.logout();
SecurityContextHolder.clearContext();
session=null;                            // session re-assigned

也许你是这个意思?

HttpSession session =request.getSession(true);
session.invalidate();
myApplication.logout();
SecurityContextHolder.clearContext();

顺便说一句,我删除了session = null,因为在Java中没有理由(C是另一回事).

BTW, I've dropped session = null since there's no reason in Java (C would be another matter) to do that.

当然,代码甚至可以更简洁:

Of course, the code could be even cleaner:

request.getSession().invalidate();
myApplication.logout();
SecurityContextHolder.clearContext();

这篇关于声纳抱怨局部变量的无用分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆