Index: /trunk/data_nodist/UnconnectedWaysTest.osm
===================================================================
--- /trunk/data_nodist/UnconnectedWaysTest.osm	(revision 6556)
+++ /trunk/data_nodist/UnconnectedWaysTest.osm	(revision 6556)
@@ -0,0 +1,33 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version='0.6' upload='false' generator='JOSM'>
+  <node id='-222' visible='true' lat='34.56990542224688' lon='-6.811381227813049' />
+  <node id='-221' visible='true' lat='34.569663967347694' lon='-6.811562128191087' />
+  <node id='-220' visible='true' lat='34.56981334217753' lon='-6.811764873147308' />
+  <node id='-219' visible='true' lat='34.569940990748705' lon='-6.8114046823157315' />
+  <node id='-218' visible='true' lat='34.56991845793937' lon='-6.81141750053251' />
+  <node id='-217' visible='true' lat='34.56975622907155' lon='-6.811807241057648' />
+  <node id='-216' visible='true' lat='34.56991204337294' lon='-6.811693873591789' />
+  <node id='-215' visible='true' lat='34.56993168009912' lon='-6.811454292115847' />
+  <way id='-226' visible='true'>
+    <nd ref='-220' />
+    <nd ref='-217' />
+    <tag k='highway' v='service' />
+  </way>
+  <way id='-225' visible='true'>
+    <nd ref='-221' />
+    <nd ref='-218' />
+    <nd ref='-219' />
+    <tag k='highway' v='service' />
+  </way>
+  <way id='-224' visible='true'>
+    <nd ref='-216' />
+    <nd ref='-220' />
+    <tag k='highway' v='service' />
+  </way>
+  <way id='-223' visible='true'>
+    <nd ref='-215' />
+    <nd ref='-218' />
+    <nd ref='-222' />
+    <tag k='highway' v='service' />
+  </way>
+</osm>
Index: /trunk/src/org/openstreetmap/josm/data/osm/Node.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/osm/Node.java	(revision 6555)
+++ /trunk/src/org/openstreetmap/josm/data/osm/Node.java	(revision 6556)
@@ -8,4 +8,11 @@
 import org.openstreetmap.josm.data.osm.visitor.Visitor;
 import org.openstreetmap.josm.data.projection.Projections;
+import org.openstreetmap.josm.tools.CheckParameterUtil;
+import org.openstreetmap.josm.tools.Predicate;
+import org.openstreetmap.josm.tools.Utils;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.TreeSet;
 
 /**
@@ -337,3 +344,35 @@
         return false;
     }
+
+    /**
+     * Tests whether {@code this} node is connected to {@code otherNode} via at most {@code hops} nodes
+     * matching the {@code predicate} (which may be {@code null} to consider all nodes).
+     */
+    public boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate) {
+        CheckParameterUtil.ensureParameterNotNull(otherNodes);
+        CheckParameterUtil.ensureThat(!otherNodes.isEmpty(), "otherNodes must not be empty!");
+        CheckParameterUtil.ensureThat(hops >= 0, "hops must be non-negative!");
+        return hops == 0
+                ? isConnectedTo(otherNodes, hops, predicate, null)
+                : isConnectedTo(otherNodes, hops, predicate, new TreeSet<Node>());
+    }
+
+    private boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate, Set<Node> visited) {
+        if (otherNodes.contains(this)) {
+            return true;
+        }
+        if (hops > 0) {
+            visited.add(this);
+            for (final Way w : Utils.filteredCollection(this.getReferrers(), Way.class)) {
+                for (final Node n : w.getNodes()) {
+                    final boolean containsN = visited.contains(n);
+                    visited.add(n);
+                    if (!containsN && (predicate == null || predicate.evaluate(n)) && n.isConnectedTo(otherNodes, hops - 1, predicate, visited)) {
+                        return true;
+                    }
+                }
+            }
+        }
+        return false;
+    }
 }
Index: /trunk/src/org/openstreetmap/josm/data/validation/tests/UnconnectedWays.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/validation/tests/UnconnectedWays.java	(revision 6555)
+++ /trunk/src/org/openstreetmap/josm/data/validation/tests/UnconnectedWays.java	(revision 6556)
@@ -114,5 +114,5 @@
         mindist = Main.pref.getDouble(PREFIX + ".node_way_distance", 10.0);
         minmiddledist = Main.pref.getDouble(PREFIX + ".way_way_distance", 0.0);
-        dsArea = Main.main.getCurrentDataSet().getDataSourceArea();
+        dsArea = Main.main == null || !Main.main.hasEditLayer() ? null : Main.main.getCurrentDataSet().getDataSourceArea();
     }
 
@@ -135,9 +135,8 @@
                         continue;
                     }
-                    // There's a small false-positive here.  Imagine an intersection
-                    // like a 't'.  If the top part of the 't' is short enough, it
-                    // will trigger the node at the very top of the 't' to be unconnected
-                    // to the way that "crosses" the 't'.  We should probably check that
-                    // the ways to which 'en' belongs are not connected to 's.w'.
+                    // to handle intersections of 't' shapes and similar
+                    if (en.isConnectedTo(s.w.getNodes(), 3 /* hops */, null)) {
+                        continue;
+                    }
                     map.put(en, s.w);
                 }
Index: /trunk/test/unit/org/openstreetmap/josm/data/validation/tests/UnconnectedWaysTest.java
===================================================================
--- /trunk/test/unit/org/openstreetmap/josm/data/validation/tests/UnconnectedWaysTest.java	(revision 6556)
+++ /trunk/test/unit/org/openstreetmap/josm/data/validation/tests/UnconnectedWaysTest.java	(revision 6556)
@@ -0,0 +1,38 @@
+// License: GPL. For details, see LICENSE file.
+package org.openstreetmap.josm.data.validation.tests;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.data.Preferences;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.projection.Projections;
+import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
+import org.openstreetmap.josm.io.OsmReader;
+
+import java.io.FileInputStream;
+
+import static org.CustomMatchers.isEmpty;
+import static org.junit.Assert.assertThat;
+
+public class UnconnectedWaysTest {
+
+    UnconnectedWays bib;
+
+    @Before
+    public void setUp() throws Exception {
+        bib = new UnconnectedWays.UnconnectedHighways();
+        Main.pref = new Preferences();
+        Main.setProjection(Projections.getProjectionByCode("EPSG:3857"));
+        bib.initialize();
+        bib.startTest(null);
+    }
+
+    @Test
+    public void testTicket6313() throws Exception {
+        final DataSet ds = OsmReader.parseDataSet(new FileInputStream("data_nodist/UnconnectedWaysTest.osm"), NullProgressMonitor.INSTANCE);
+        bib.visit(ds.allPrimitives());
+        bib.endTest();
+        assertThat(bib.getErrors(), isEmpty());
+    }
+}
