From d1b597e2899a1925ada54b347075da6ec9bf4f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20K=C3=BChne?= Date: Sun, 29 Jun 2003 10:41:47 +0000 Subject: [PATCH] added tests for StringLiteralEquality --- .../coding/InputStringLiteralEquality.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java new file mode 100644 index 000000000..2d650ae30 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/coding/InputStringLiteralEquality.java @@ -0,0 +1,47 @@ +package com.puppycrawl.tools.checkstyle. + +/** + * Input file for the StringLiteralEqualityCheck + * @author Lars Kühne + */ +public class InputStringLiteralEquality +{ + void foo(String name) + { + if (name == "Lars") + { + // flagged, should use equals + } + + if ("Oleg" == name) + { + // flagged, should use equals + } + + if ("Oliver" == "Oliver") + { + // doesn't make much sense because this can be evaluated + // to true at compile time, but is flagged anyway + } + + String compare = "Rick"; + if (name == compare) + { + // currently not flagged. + // + // Implementing this is very complicated, we would need + // - type info on the == operands + // - prevent false alarms where the user explicitly wants + // to compare object identities + // + // My current feeling is that we should leave finding + // this one to manual code inspections. After all MCI is + // what some of us get paid for :-) + } + + if ("Rick".toUpperCase() == "Rick".toLowerCase()) + { + // completly dynamic, don't flag + } + } +}